I am passing a value to the dropdownlist, how to get the index of the value of the dropdownlist and set it using javascript?
document.getElementById("ddlColors").selectedIndex = ?
Thank you
I am passing a value to the dropdownlist, how to get the index of the value of the dropdownlist and set it using javascript?
document.getElementById("ddlColors").selectedIndex = ?
Thank you
Share Improve this question asked Sep 29, 2012 at 22:38 BenkBenk 1,3126 gold badges36 silver badges64 bronze badges 1- 1 Not sure what you wish to do here? Your Javascript looks OK for setting the index if ddlColors is a <select> element. – Steve Chambers Commented Sep 29, 2012 at 22:46
2 Answers
Reset to default 6A loop is the only way, really:
var options = document.getElementById('ddlColors').options;
for(var i = 0; i < options.length; i++) {
if(options[i].value === someValue) {
options[i].selected = true;
break;
}
}
Here's a demo.
I think I misunderstood your question cause if I didn't the answer is too pretty forward: use the value http://jsbin./urezor/2/edit