Maxi
Maxi

Reputation: 743

Perform .each function after first table row

I have the following code

 $("#description tr").each(function(){
            var description;
            var chargecode;
            var amount=0;

.......the code continues but what I am interested in is to do the each function after the first row. My first table row has the titles so my code keeps getting null variable values and so on. Is this possible or should I just separate the title row as a different table? All help appreciated

Upvotes: 2

Views: 927

Answers (5)

Jakub Hampl
Jakub Hampl

Reputation: 40543

Use $("#description tr:gt(0)").each. The :gt(0) stands for "whose index is greater then 0" and it will filter the matched elements by their index in occurring on the page. See the docs.

Upvotes: 1

Ilia G
Ilia G

Reputation: 10211

The parameter to the each() callback is the index. You could use that or you could define you table properly with <thead> and <tbody> and then use $("#description tbody tr")

Upvotes: 2

ipr101
ipr101

Reputation: 24236

Try -

 $("#description tr:gt(0)").each(function(){
            var description;
            var chargecode;
            var amount=0;

The tr:gt(0) should only get rows with in index higher than zero, therefore ignoring the first row.

Demo http://jsfiddle.net/ipr101/rPMUh/

Upvotes: 1

Bas Slagter
Bas Slagter

Reputation: 9929

You can either add a class to the first row (or to all the others) and filter on that (look at the not-selector or you can use the index of the loop (which is the first parameter) which increments on every iteration and than you simple skip the first one (or you add your own counter). Of course, a better solution is:

$("#description tr:not(:first-child)") 

Which uses both the not-selector as the first-child selector. You can also use the greater than selector:

$("#description tr:gt(0)")

So many options....I would go for the last one.

Upvotes: 1

Kobi
Kobi

Reputation: 138017

You can use:

$("#description tr:not(:first-child)")

Working example: http://jsfiddle.net/KntKA/

Another option is $("tr:nth-child(n+2)"), but it doesn't read quite as nicely.

Upvotes: 1

Related Questions