Bob_Kruger Bob_Kruger - 1 year ago 61
jQuery Question

jQuery selector for every row except the first on every table except first

I want to apply right alignment on the last cell of every table row, skipping the first table on the page and skipping the first row of every table.

I wrote the following:

$("table:gt(0) tr:gt(0) td:last-child").css("text-align", "right");

  1. The tables after the first are selected. GOOD.

  2. The last cell in each row is aligned right. GOOD.

  3. The first row in the first table in the result set is skipped. GOOD.

  4. Each first row in subsequent tables in the result set has the style applied. BAD.

I've tried passing a function to the "each" method on the wrapped set, but that isn't working. Any ideas?

Answer Source

You were almost there.

$("table:gt(0)").find("tr:gt(0) td:last-child").css("text-align", "right");

otherwise, tr:gt(0) is always true because jquery is looking at each tr, not each tr in each table.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download