views:

1971

answers:

5

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?

Thanks!

A: 

There is probably a better way, but one idea may be to add a class to the first row of each table that you do not want to have the style and filter those out with a not(.myclass).

Likewise add classes to the tables that you do want the styles to go to.

Tom Hubbard
+1  A: 

what's wrong with using the DOM to select your tables, and using jQuery to apply your styles to the tables? too many lines of code?

ryansstack
+1  A: 
$("table:gt(0) tr").each(){
     if($(this).not(":first-child")){
           $(this).find("td:last-child").css("text-align","right");
     }
}

not the best way probably, but this is how I would do it.

GSto
Thanks, that looks like a good approach too. I tried something similar but didn't get it quite right.
Bob_Kruger
+1  A: 

try

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

as the gt(0) only refers to the index in the jQuery object that you're building, not the table.

cobbal
That works too. Thanks.
Bob_Kruger
+9  A: 

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.

ScottE
Awesome! That did it.
Bob_Kruger