Search code examples
jqueryhtml-tableparentsiblings

JQuery - grab the td value next to radio button


I have the following dilemma: My HTML is as such (this exists within a PL/SQL app.):

<table id="search_result_table">
<tr>

<input type=radio name="pv_select" value="'||lv_spriden_id||'"/>
<font face="Calibri" size=3><td id="spriden_id"     name="spriden_id">'||lv_spriden_id||'</td></font>'
<font face="Calibri" size=3><td id="last_name" class="c_last_name"     name="last_name">'||lv_spriden_last_name||'</td></font>
<font face="Calibri" size=3><td id="first_name" class = "c_first_name"     name="first_name">'||lv_spriden_first_name||'</td></font>
</tr>
</table>

I am able to get at the selected radio button value via:

$("input[name=pv_select]:checked").val()

However, I would like to be able to get the value of the "last_name" column cell (which exists within a table next to the radio button). How can I retrieve this value (for the selected row via the radio button) via jQuery?

I've tried several things, but none are working:

$("input[name=pv_select]:checked").parent().siblings("td:eq(1)").text());    

Thanks in advance.


Solution

  • The HTML is invalid; not sure if that’s mucking up your DOM tree, and thus confusing jQuery as it tries to traverse the broken DOM tree.

    Here’s valid HTML:

    <table id="search_result_table">
        <tr>
            <td>
                <input type=radio name="pv_select" value="'||lv_spriden_id||'"/>
            </td>
            <td id="spriden_id" name="spriden_id"><font face="Calibri" size=3>'||lv_spriden_id||'</font></td>
            <td id="last_name" class="c_last_name" name="last_name"><font face="Calibri" size=3>'||lv_spriden_last_name||'</font></td>
            <td id="first_name" class = "c_first_name" name="first_name"><font face="Calibri" size=3>'||lv_spriden_first_name||'</font></td>
        </tr>
    </table>
    

    And jQuery to get the value of the “last name” table cell in the same row as the checked radio button:

    $("input[name=pv_select]:checked").parent().siblings("td.c_last_name").text());
    

    (I say the HTML is valid: values for the id attribute are meant to be unique within an HTML page, so if you have multiple rows using the same id attributes for the table cells, that’s invalid too.)