RussellHarrower
RussellHarrower

Reputation: 6810

jQuery - $.each find if first run of $.each

I am not sure if I can do this or not: I need to know how to find out if the json $.each is running for the first time.

So say I have 14 items in the JSON the $.each goes though 14 times I want to do something different to the first time the $.each run.

For example, say the first return is the letter A, and in the array we have A,B,C,D,E,F.

I want to bold the A but none of the others.

Upvotes: 11

Views: 20125

Answers (1)

mu is too short
mu is too short

Reputation: 434675

The callback function for $.each gets the index as its first parameter:

$.each(collection, function(index, value) {
    // index will be zero on the first one
});

Upvotes: 31

Related Questions