0

using jquery how do alter the css of the last tr with a th

$("#mytable tr").has('th').last().parents('tr').css({'color':'blue'});

HTML

<table border="1" id="mytable">
    <tr>
        <th>row 1, cell 1</th>
        <th>row 1, cell 2</th> 
    </tr>
    <tr> 
        <th>row 1, cell 1 - change this</th>
        <th>row 1, cell 2 - change this</th> 
    </tr>
    <tr>
        <td>row 2, cell 1</td>
        <td>row 2, cell 2</td>
    </tr>
</table>
BoltClock
  • 665,005
  • 155
  • 1,345
  • 1,328
Hello-World
  • 8,457
  • 22
  • 78
  • 151

2 Answers2

6

As an alternative to xdazz's answer, I'd offer:

$('tr:has(th):last').css('color','blue');

References:

Community
  • 1
  • 1
David Thomas
  • 240,457
  • 50
  • 366
  • 401
  • +1 I never really understood if quotes had any special meaning within `:has()` in jQuery (I know they're invalid within `:not()`). – BoltClock Sep 18 '12 at 07:25
  • I'm never quite sure myself; I tend to include them simply because it *feels* right. – David Thomas Sep 18 '12 at 07:26
  • It feels wrong to me, as you're passing a selector rather than a string :/ Having quotes on `:has()` but not on `:not()` seems inconsistent to me as well... – BoltClock Sep 18 '12 at 07:27
  • ...and edited (having looked at the [API](http://api.jquery.com/has-selector/), in which it's *implied* that the quotes are unnecessary; but not explicitly stated either way. – David Thomas Sep 18 '12 at 07:30
  • Apparently, quotes *are* allowed, both in `:not()` and `:has()`. I've posted a question asking why: http://stackoverflow.com/questions/12475595/why-do-jquerys-not-and-has-selectors-allow-quoted-arguments – BoltClock Sep 18 '12 at 10:57
5

Find the last th, and its parent should be the last tr who has th.

$("#mytable th:last").parent().css('color', 'blue');
xdazz
  • 154,648
  • 35
  • 237
  • 264