tags:

views:

26

answers:

2

I'm using gt() to add columns to a table.

I need to use the row number and the column number to build the cell id.

Getting the column number is straightforward:

var c = $("#gridLayout tr:first td").length;

but how can I get the current index from gt() for the row number?

$("#gridLayout tr:gt(0)").append('<td>.......</td>');
+2  A: 

Im not really sure what your goal here is but something similar to the following would return the row index and append whatever contents:

$('#gridlayout tr').each( function() {
    $(this).append('<td> Things.... </td>');
    alert($(this).index());
})

If you post some more details I will gladly provide further info.

HurnsMobile
That sorted it thanks!
Leo
+3  A: 

One way would be to use the built in index parameter for .each().

This lets you get an index number without having to call another method.

Try it out: http://jsfiddle.net/8V4AY/

   // Reference index --------------v
$("#gridLayout tr").each(function( idx ) {
       // idx contains the current index number,
       //    without having to call another method

     $(this).append('<td>.......</td>');
});

If you were going to skip the first row:

$("#gridLayout tr:gt(0)")...

you could adjust the idx by 1 if you need

idx++;

http://jsfiddle.net/8V4AY/1/

patrick dw
Touche Patrick! I do believe your solution is more elegant - you get my vote.
HurnsMobile
@Hurns - Thanks for the vote. :o) In all honesty, your solution was the first that came to mind, so when you beat me to it, I had to think of something else. Overall, I think this may be a little quicker.
patrick dw
Great thanks - you get the tick for introducing me to jsfiddle!
Leo
@Leo - You're welcome. :o) jsFiddle *is* pretty slick.
patrick dw