Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do something different with first element in each loop?

Tags:

jquery

each

Within an each() loop, is it possible to do something with the first element in particular, not the next ones? Something like this:

$( '.selector').each(function(){
    // if first element found, do something
});
like image 722
drake035 Avatar asked Feb 16 '15 20:02

drake035


1 Answers

$('.selector').each(function(i, el){
    if ( i === 0) {
       // Will be done to first element.
    }

});
like image 166
void Avatar answered Oct 04 '22 05:10

void