jquery button onclick get parent select option selected

13,687

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();
Share:
13,687
Morten Repsdorph Husfeldt
Author by

Morten Repsdorph Husfeldt

HTML, CSS, Javascript, PHP, MySQL... and DynamicWeb Full stack web developer

Updated on June 07, 2022

Comments

  • Morten Repsdorph Husfeldt
    Morten Repsdorph Husfeldt almost 2 years

    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);
    });