On Jun 17, 3:46 am, theprodigy <tigerseyet...@gmail.com> wrote:
> I've been trying for a while to alter the second to last row of a
> table. I've tried several ways. The number of rows is dynamic so I
> can't hard code a number into nth-child. I used $rowNeeded =
> $thisRow.parents('table:first').children().children().length - 1 to
> get the second to last row, but it doesn't seem like I can pass this
> variable into nth-child either.
>
> How can I select the second to last row of a table?

In browsers compliant with the W3C DOM 2 HTML specification, table
elements have a rows collection that contains all the rows in the
table.  That collection has a length attribute, so, where - table - is
a reference to a table element:

  var rows = table.rows;
  var secondLastRow = rows[rows.length - 2];

Since rows is a live collection, you can get a reference once and keep
it, it will dynamically updated regardless of how many rows are added
or removed from the table.  Note that the above will error if there
are less than two rows in the table, use with care.


--
Rob

Reply via email to