Frontendplace Blog

Sharing Frontend developing ideas

Jquery set selectedIndex

Posted by info on July 2nd, 2008

In jquery you can’t set the selectedIndex of a selectbox with $(…).selectedIndex = indexValue;for instance set the selected index of the first selectbox inside the form with id ‘myForm’: you can use the function each() like this:$(‘#myForm select[0]‘).each(){ function(this().selectedIndex = 0;)}if you know the id of the selectbox its easier to use the  DOM notation document.getElementById(‘mySelectinput’);You can get the selected option with the path expression  #myForm select[0] option:selected