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

RussellHarrower picture RussellHarrower · May 22, 2011 · Viewed 18k times · Source

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.

Answer

mu is too short picture mu is too short · May 22, 2011

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
});