crazymatt
crazymatt

Reputation: 3286

Use JQuery to get a specific TD in a series of Tables

I have a mess of HTML TABLE tags and I am trying to use JQuery to drill down to a specific TD cell within all the generated TABLE's. The problem is I have JQuery getting the correct TD cell but its only getting the first cell in a series of TABLE's with the same structure.

Here is the JQuery that I am using that gets to the correct TD:

<script type="text/javascript">
    $(function ($) {
        $(".dataContent div table tbody tr td").first().css("background", "yellow");
    });
</script>

Here is the HTML structure simplified:

<td class="dataContent">
    <a href="#"></a>
    <div>
        <table>...</table>
        <div>
            <table cellspacing="0" cellpadding="0" style="border-width:0;">
                <tbody>
                    <tr>
                        <td><a href="#"><img /></a></td><!--this cell-->
                        <td><div>random cell</div></td>
                    </tr>
                </tbody>
            </table>
            <!--Generated Tables with same structure as the TABLE above-->
            <table></table>
            <table></table>
            <table></table>
            <table></table>
        </div>
    </div>
</td>

So I not only want to get the first cell in the first TABLE but also in all the generated TABLES below. Any help would be appreciated.

Upvotes: 0

Views: 2766

Answers (2)

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114367

$(".dataContent div table").eq(0).find('td').eq(0) gives you the first TD in the first table.

$(".dataContent div table").length will give you the number of tables.

Make a loop. easy.

Upvotes: 1

BoltClock
BoltClock

Reputation: 723638

Use td:first-child instead of .first() to get all cells that are the first in their rows within every table:

$(".dataContent div table tbody tr td:first-child").css("background", "yellow");

Using the :first selector or the .first() method causes jQuery to return only the very first element out of all the elements that are matched, so it never looks beyond that one cell.

Upvotes: 1

Related Questions