jquery button onclick get parent select option selected

I am trying to get the value of a parent when clicking a button:

The HTML part:

<td>
    <select>
        <option value='2' >Administrator</option>
        <option value='3' selected='selected'>Webmaster</option>
        <option value='4' >Editor</option>
        <option value='5' >Journalist</option>
    </select>
</td>
<td>
    <div class='save'>Save</div>
</td>

The JavaScript part:

$('.save').click( function() {
    level = $(this).parent('select option:selected').val();
    alert(level);
});

Upvotes: 3

Views: 14578

Answers (1)

techfoobar
techfoobar

Reputation: 66693

The manner in which you are getting the selected value is incorrect. You should instead do:

level = $(this).parent().prev().find('select option:selected').val();

Upvotes: 6

Related Questions