Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select table cells without selecting nested table cells in jQuery

I want to select only the first level of 'td' elements in a table and not the cells of any nested tables. eg:

<table id="Outer">
    <tr>

        <td> --this one
        </td> 

        <td> --this one
            <table>
                <tr>
                    <td></td> -- but not this one or any deeper nested cells
                </tr>
            </table>
        </td>

    </tr> 
</table>

(and yes in prod code i would include tbody, thead...)

like image 354
missaghi Avatar asked Dec 29 '25 11:12

missaghi


1 Answers

I'd use the children selector, which only selects the immediate children matching the expression. To make it easy to select just the outer table, I'd give it a name. NOTE: this won't work with your sample as I've added in the selectors for thead, tbody, and tfoot as you indicate you will have in production. Adjust accordingly for your sample.

$('table#namedTable').children('tbody,thead,tfoot')
                     .children('tr')
                     .children('td')
like image 125
tvanfosson Avatar answered Dec 31 '25 06:12

tvanfosson



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!