This is causing me a lot of grief, and I’m almost positive it’s something stupid.
Why is this returning undefined instead of “Test”?
See JSFiddle
<fieldset class="fieldset">
<select class="list">
<option selected="selected">Test</option>
</select>
</fieldset>
<script type="text/javascript">
alert($('.fieldset').children('select.list option:selected').val());
</script>
You needed to use
findinstead ofchildren.childrengives you the immediate descendants of your selector, i.e. only one level down.findsearches all descendants. Otherwise, you were spot on!(Keep in mind that if you have more than one
selectin yourfieldset, your current selector will get them all at once. If this is not what you want, use something that gets theselectyou want specifically; in your example it would be$('.list').children('select.list option:selected').val()