santa
santa

Reputation: 12512

Targeting first TD but not in 1st row w/ jQuery

I need to add css to the first column of the the table, all but in the first row.

Can I do something like: $(".myTbl tr td:not(:nth-child(1))").css(...

Upvotes: 0

Views: 92

Answers (3)

tvanfosson
tvanfosson

Reputation: 532435

More like:

$('.myTbl tr:not(:first) td:first').css( ... )

That is, the first td in each row which is not the first row in a table but only in tables with the class myTbl. You may want to also specify whether you're considering header rows as distinct from body rows. If you have them separated into header/body sections and only want the body rows you can get that with.

$('.myTbl tbody tr td:first').css( ... )

Note: I'm not addressing nested tables with this. If you have nested tables you'll probably want to be more explicit, using direct children rather than ancestor/descendant relationships.

$('.myTbl > tbody > tr > td:first').css( ... )

Upvotes: 1

Josh
Josh

Reputation: 12566

You could put your first row in a <thead>, then just target the first <tr> in your <tbody>.

<table class="mytable">
  <thead>
    <tr>
      <th>Something</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data</td>
    </tr>
  </tbody>
</table>

$(".mytable tbody tr td:first").css("something", "else");

Upvotes: 0

reedlauber
reedlauber

Reputation: 371

Are you ignoring the first row because it is a header row? Is it possible to put that first row in a and the add your css to rows inside ?

Upvotes: 0

Related Questions