Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
});
like image 346
Morten Repsdorph Husfeldt Avatar asked Dec 06 '25 14:12

Morten Repsdorph Husfeldt


1 Answers

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();
like image 196
techfoobar Avatar answered Dec 10 '25 08:12

techfoobar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!