morne
morne

Reputation: 4179

add class with JQuery traversing

How would you go about adding column specific classes to the table below with JQuery traversing? ( last column in my case )

i started to dab around quite hoplessly with something along the line of

$('#myID tr').children("td:not(:first)").addClass("me");

like I said, hopelessly. Please pass me some help.

<table id="myID" class="myClass">
    <tr>
        <th>one</th>
        <th>two</th>
        <th>three</th>
        <th>four</th>
    </tr>
    <tr>
        <td>1</td>
        <td>2</td>
        <th>3</th>
        <th>4</th>
    </tr>
    <tr>
        <td>a</td>
        <td>b</td>
        <th>c</th>
        <th>d</th>
    </tr>
</table>

SOLUTION:

$('#myID tr').children("td:nth-last-of-type(1)").addClass("me");

Upvotes: 0

Views: 65

Answers (1)

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

Try first-of-type instead

$('#myID tr').children("td:not(:first-of-type)").addClass("me");

Or first-child

$('#myID tr').children("td:not(:first-child)").addClass("me");

Demo

Upvotes: 2

Related Questions