views:

40

answers:

1

Lets say I have the following:

<table><tr><td>
    <table> <!-- I want a reference to this table -->
        <tr>
            <td>
                <table>
                    <tr><td><span class="theSpanClass"></span></td></tr>
                </table>
            </td>
        </tr>
     </table>
</td></tr></table>

Using jQuery, I'd like to be able to reference the 2nd table starting from the span towards the body.

Thanks

+4  A: 

For original question: If you're coming from the span, then you can do this:

$(".theSpanClass").parents("table:last")

This uses .parents() to get all ancestors that are <table>, then selects the :last one it finds, since they're ordered going up the DOM.


Updated for new question: Since the question's been updated, that's 2 levels up, which you would find using :eq() like this:

$(".theSpanClass").parents("table:eq(1)") //it's 0-based
Nick Craver
@Nick: awsome!, thanks Nick
DanC