Dynamically set the VALUE of an text-INPUT element to default by clicking SELECT option

Chris Ha

Ok, here is the first thing that what I want to do:

1) Lets say a user has been adding text to one or to both of the input fields. Now, if the user is either selecting option value 'Name1' or option value 'Name2' of the select field the input fields value have to be reseted. How can this be done?

<div id='CustomerIsNotInDatabase'>
    <input id='vlastname' name='ulastname' type='text' value=''></input>
    <input id='vfirstname' name='ufirstname' type='text' value=''></input>
</div>

<div id='customerIsInDatabase'>
<select>
    <option selected='true' value=''>Choose</option>
    <option value='Name1'></option>
    <option value='Name2'></option>
</select>
</div>

2) Now the other way round: If either option value 'Name1' or option value 'Name2' is selected: How can this value be reseted to the 1st option with value '' if the user adds text to an input field?

Zakaria Acharki

1. For the first question.

If the user is either selecting option value 'Name1' or option value 'Name2' of the select field the input fields value have to be reseted

You can use change event to handle the user change and reset input using val('') :

$('#customerIsInDatabase select').change(function(){
     if ( $(this).val() != '' )
     {
         $('#vlastname').val('');
         $('#vfirstname').val('');
     }
});

2. For the second question.

If either option value 'Name1' or option value 'Name2' is selected: How can this value be reseted to the 1st option with value '' if the user adds text to an input field?

You can use keyup event to detect user action when any key presed in input field :

$('#CustomerIsNotInDatabase input').keyup(function()
{
      $('#customerIsInDatabase select').val('').change();
});

Hope this helps.


Full Example :

$('#customerIsInDatabase select').change(function()
{
     if ( $(this).val() != '' )
     {
         $('#vlastname').val('');
         $('#vfirstname').val('');
     }
});


$('#CustomerIsNotInDatabase input').keyup(function()
{
      $('#customerIsInDatabase select').val('').change();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='CustomerIsNotInDatabase'>
    <input id='vlastname' name='ulastname' type='text' value=''></input>
    <input id='vfirstname' name='ufirstname' type='text' value=''></input>
</div>

<div id='customerIsInDatabase'>
<select>
    <option selected='true' value=''>Choose</option>
    <option value='Name1'>Name1</option>
    <option value='Name2'>Name2</option>
</select>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically set the VALUE of an text-INPUT element to default by clicking SELECT option

From Dev

jquery select option dynamically assign value to another 2 input text

From Java

how to use ng-option to set default value of select element

From Dev

Default Select Option Based on Input Value

From Dev

Jquery set value of option element to be the text of the element

From Dev

Set default value of select to null option

From Dev

how to set a default value for select option

From Dev

Dynamically set the text of a select element, by its ID

From Dev

Match an input text with a value of an option and auto select it

From Dev

Dynamically add and/or subtract value from SELECT OPTION to an existing INPUT value

From Dev

Set default value of select2 input

From Dev

Set default value for select input in php

From Dev

Set default option in select-element using php

From Dev

CakePHP2 - Default value for input - select with option multiple

From Dev

set selected value of a select input to another selects option value

From Dev

set default option in a select via ng-init on VALUE and not INDEX

From Dev

Laravel - Select - set Default option

From Dev

JQuery change select option when text input like value

From Dev

How to Exchange Value Between Select Option and Text Input On Conditional

From Dev

Dynamically align text. Based on select input value

From Dev

Use JQuery to set the select option value and text from Array

From Dev

Use JQuery to set the select option value and text from Array

From Dev

Set the value and text of a select option from Javascript table

From Dev

Jquery select option default option text

From Dev

How to set default value on an input box with select2 initialized on it?

From Dev

Insert Input text in a Select as option

From Dev

Set selected option by text and reset to default option

From Dev

Set default value for select html element in Jinja template?

From Java

How can I set the default value for an HTML <select> element?

Related Related

  1. 1

    Dynamically set the VALUE of an text-INPUT element to default by clicking SELECT option

  2. 2

    jquery select option dynamically assign value to another 2 input text

  3. 3

    how to use ng-option to set default value of select element

  4. 4

    Default Select Option Based on Input Value

  5. 5

    Jquery set value of option element to be the text of the element

  6. 6

    Set default value of select to null option

  7. 7

    how to set a default value for select option

  8. 8

    Dynamically set the text of a select element, by its ID

  9. 9

    Match an input text with a value of an option and auto select it

  10. 10

    Dynamically add and/or subtract value from SELECT OPTION to an existing INPUT value

  11. 11

    Set default value of select2 input

  12. 12

    Set default value for select input in php

  13. 13

    Set default option in select-element using php

  14. 14

    CakePHP2 - Default value for input - select with option multiple

  15. 15

    set selected value of a select input to another selects option value

  16. 16

    set default option in a select via ng-init on VALUE and not INDEX

  17. 17

    Laravel - Select - set Default option

  18. 18

    JQuery change select option when text input like value

  19. 19

    How to Exchange Value Between Select Option and Text Input On Conditional

  20. 20

    Dynamically align text. Based on select input value

  21. 21

    Use JQuery to set the select option value and text from Array

  22. 22

    Use JQuery to set the select option value and text from Array

  23. 23

    Set the value and text of a select option from Javascript table

  24. 24

    Jquery select option default option text

  25. 25

    How to set default value on an input box with select2 initialized on it?

  26. 26

    Insert Input text in a Select as option

  27. 27

    Set selected option by text and reset to default option

  28. 28

    Set default value for select html element in Jinja template?

  29. 29

    How can I set the default value for an HTML <select> element?

HotTag

Archive