Tim
Tim

Reputation: 1249

jquery .each misses the first row

I have the following in a page the var rows shows length=1 but then it reaches the .each statement it doesn't enter it. I must be missing something. I only have this problem when there is only 1 row in the table. Any thoughts?

$('.addnewrow').click(function() {
        var rows = $('#webgrid tr:gt(0)');
        rows.each(function (index) {
           // do something 
        });

       // now do something else based on the results of the .each
    });

Upvotes: 0

Views: 237

Answers (2)

PhilTrep
PhilTrep

Reputation: 1641

In #webgrid tr:gt(0), you are selecting every tr which has the ancestor #webgrid AND is in a index position GREATER THAN 0 (gt(0)), in this case, using the :gt() pseudo selector is useless if you want to iterate through all the tr. Since gt(0) means "every element that is at a index above 0" and you only have one element at position 0, you are missing it alltogether.

Hope this helps!

Upvotes: 1

PSL
PSL

Reputation: 123739

:gt() takes index which is 0 based and here you are asking for everything greater than the first element. So when there is only row you don't have any other tr's after the first one so the statement $('#webgrid tr:gt(0)') returns empty collection.

Upvotes: 2

Related Questions