0

i have 2 dropdownlist, i want if i select Alabama in dropdwon1 the selected value in dropdwon2 should change in to Other.., how to do this using JQuery

<select id="dropdwon1">
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">Arizona</option>
</select>

<select id="dropdwon2">
<option value="3">Item1</option>
<option value="4">Item2</option>
<option value="5">Other..</option>
</select>
  • welcome to SO. you will get a lot more help if you show us what you have already tried. folks are willing to help but not until you have shown some effort on your part – Semicolons and Duct Tape May 05 '16 at 00:16
  • Possible duplicate of [Populate Dropdown list based on another dropdown list](http://stackoverflow.com/questions/12258839/populate-dropdown-list-based-on-another-dropdown-list) – Willie Cheng May 05 '16 at 00:57
  • check this [URL](http://stackoverflow.com/questions/5686735/populate-one-dropdown-based-on-selection-in-another) – Willie Cheng May 05 '16 at 00:58

2 Answers2

0

You must bind event to first select change event and check it's selected value and set value for second select like this:

//Bind change event to first select
$('#dropdwon1').on('change', function(){

    //check selected value
    var value = $(this).val();
    if(value == 1){

         //Set selected value for second select
         $('#dropdwon2').val(5);
    }
})
Shirin Safaeian
  • 950
  • 6
  • 18
  • i have list of States in my dropdwon1 and in dropdwon2 list of County,,what i need is when i select any state other than Alabama in dropdwon1 the selected value in dropdwon2 should be other.. – myo1981 May 04 '16 at 21:28
0

You have to initialize your value for the second select for the first value of your state select, like so :

if($("#dropdown1").val()=="1"){
    $("#dropdown2").val("5"); // Set the value (your answer)
} 

then use the change event and give the different values of the second select for the states values.

As the user can select Alabama again after having chosen another value, you have to put the code for the first value in the change event function.

Here is a jsfiddle :

https://jsfiddle.net/mantisse_fr/ak81o26s/4/

Mantisse
  • 309
  • 4
  • 15