Search code examples
jqueryfieldset

Get selected option from fieldset


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>

Solution

  • You needed to use find instead of children.

    children gives you the immediate descendants of your selector, i.e. only one level down. find searches all descendants. Otherwise, you were spot on!

    (Keep in mind that if you have more than one select in your fieldset, your current selector will get them all at once. If this is not what you want, use something that gets the select you want specifically; in your example it would be $('.list').children('select.list option:selected').val()