views:

537

answers:

2

i want to dynamically add a class to all rows of a table except the first and last row. how would i do this without assigning a css class to the rows to identify them. I am getting all but the first row currently with

$("#id").find("tr:gt(0)")

i need to combine this with not("tr:last") somehow maybe?

+8  A: 
Tatu Ulmanen
this works - unless the last row has a table in it - which is the case. this is a server control so can't easily be changed. any ideas?<table id="tbl"><tr><td>header</td><td>header</td></tr><tr><td>one</td><td>one</td></tr><tr><td>two</td><td>two</td><tr><td colspan="2"><table border="0"><tr> <td>footer one</td><td>footer two</td><td>footer three</td><td>footer four</td></tr></table></td></tr></table>
Sure, use `$('#id > tr')` instead of `$('#id tr')`. That forces the selector to select only the immediate children. This requires that the `#id` element is a `<table>`.
Tatu Ulmanen
thanks for the response. with the table html i posted in first comment, `$('#tbl > tr').not(':first').not(':last').addClass("highlight");` for example, does not seem work nor does `$('table#tbl > tr').not(':first').not(':last').addClass("highlight");`
@user210757, I've updated my answer with an solution.
Tatu Ulmanen
tatu - i marked this as the answer - thanks for all the help. i had no idea about tbody - i wonder which browsers do this. seems crazy to have to handle a tag that is not there
A: 

Strange the suggestions posted did not work, they should all work! BUT...

If it did not work, do it this way.... slightly slower but MUST WORK!! TRY:

$('table#tbl tr').addClass('highlight');
$('table#tbl tr:first-child').removeClass('highlight');
$('table#tbl tr:last-child').removeClass('highlight');
Stuart G