How to set select options in jquery

By: toleronos Date: 03.06.2017

Thanks for the comment,. Keep in mind the. If you would like to select a value based on the item's value and not the index of that item then you can do the following:. What's important to understand is that val for a select element returns the value of the selected option, but not the number of element as does selectedIndex in javascript. It's not all that efficient.

The attribute "selected" is not how you specify a selected radio button in Firefox and Chrome at least. Use the "checked" attribute:. The pure javascript selectedIndex attribute is the right way to go because,it's pure javascript and works cross-browser:. Here is a jsfiddle demo with two dropdowns using one to set the other:.

javascript - Adding options to a