How do I find the highest level TR that contains a specific ID nested in a table

Posted by Mykroft on Stack Overflow See other posts from Stack Overflow or by Mykroft
Published on 2012-10-23T16:44:35Z Indexed on 2012/10/23 17:00 UTC
Read the original article Hit count: 160

Filed under:
|

I have some HTML that looks like this (NOTE: I know this code isn't great but I didn't design it originally and some of it is auto generated):

<table id="tab1">
    <tr>
        <td><span>Some text</span></td>
        <td><span>more text</span></td>
        <td><input type="text" id="inp1" onclick="DoSomething(this);" /></td>
    </tr>
    <tr>
        <td><span>Some text</span></td>
        <td><span>more text</span></td>
        <td><table id="tab2">
            <tr><td><input type="radio" id="inp2" onclick="DoSomething(this);" /><span>item</span></td></tr>
        </table></td>
    </tr>
</table>

All of that is embedded in a table which is also embedded in a table and so on. In the DoSomething(this) function I want to retrieve the TR underneath the table tab1. I'm having trouble figuring out the jquery necessary for this. Currently I'm doing something like this:

function DoSomething(control) {
    var parentTab = '<%=tab1.ClientID %>';
    var tr = $('#' + parentTab + ' > tbody > tr').has('#' + $(control).attr('id')).get(0);
}

This seems really messy but works. Is there a cleaner way to do this? If it helps the input inside the table will always be a radio button and a radio button will never appear outside of a sub table. Ideally I'd like to do this without having to know the id of tab1 but that seems impossible.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about ASP.NET