Using jQuery, how do you check if there is an option selected in a select menu, and if not, assign one of the options as selected.
(The select is generated with a maze of PHP functions in an app I just inherited, so this is a quick fix while I get my head around those :)
While I'm not sure about exactly what you want to accomplish, this bit of code worked for me.
No need to use jQuery for this:
Easy! The default should be the first option. Done! That would lead you to unobtrusive JavaScript, because JavaScript isn't needed :)
Unobtrusive JavaScript
This is another way of checking an option is selected or not in jquery. This will return Boolean (True or False).
[1] is index of select box option
Look at the selectedIndex of the
select
element. BTW, that's a plain ol' DOM thing, not JQuery-specific.