Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi,

Please any one help below requirements. It would be very useful for me . Advance Thanks

"change dropdownlist selected index with javascript"

for example I have two dropdown list in form , If I click values in first dropdown list. automaticaly respective values to show the scecond in drop down list .

A- dropdown list B- drop down list

1 4
2 5
3 6


for example If I click values "3" in first dropdown list. automaticaly respective values to show "6" the scecond in drop down list

This For your Reference

A-drop downlist B-drop downlist
3 6
Posted
Updated 31-May-16 3:27am

1 solution

Match the text value with the value you are given. If it is matched with the text of the option you can set the selectedIndex to that.

var dropdownlistbox = document.getElementById("DropdownID")

for(var x=0;x < dropdownlistbox.length -1 ; x++)
{
   if(givenValue == dropdownlistbox.options[x].text)

      dropdownlistbox.selectedIndex = x;
}


if you know the index then directly you can give like this
document.getElementById("DropDownID").selectedIndex = x;
 
Share this answer
 
v2
Comments
RaviRanjanKr 5-Aug-11 14:01pm    
My 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900