Wanting to learn
Wanting to learn

Reputation: 468

skip first element using .append()

I'm trying to skip the first I'm loading up with .append() I'm not sure how to do it.

here is the code I'm using.

 $.each(get_images, function(i,img){
  $('#container ul').append('<li><img src="'+img+'"/></li>'); 
});

Upvotes: 1

Views: 645

Answers (3)

Felipe Oriani
Felipe Oriani

Reputation: 38598

You can use slice method to skip 1 element, for sample:

$(get_images).slice(1).each(function(i, img) {
   $('#container ul').append('<li><img src="'+img+'"/></li>'); 
});

Or also, you can check the index:

$.each(get_images, function(i, img) {
  if (i > 0) {
     $('#container ul').append('<li><img src="'+img+'"/></li>'); 
  }
});

Upvotes: 1

Phil
Phil

Reputation: 11175

If you mean that you are trying to skip the first element in the $.each, I would do something like:

$.each( get_images, function( index, img ) {
    if( index > 0 ) {
        // this skips the first index of the $.each loop
    }
}

Some of the comments suggest that you wanted to append it after the first li tag, if that is the case, try what @Tushar suggested.

Upvotes: 0

Tushar Gupta
Tushar Gupta

Reputation: 15913

you can use the following code its an example:

not(':first-child')

like

$('ul li').not(':first-child').each(function ()
    {
       /// your code

    });

Upvotes: 2

Related Questions