exhtml
12/2/2016 - 6:21 AM

jQuery .each() usage (add class sequentially, etc)

jQuery .each() usage (add class sequentially, etc)


/*
Explicitly iterate over a selection: .each() method lets you do this. In the following code, we use it to add a <b> tag at the beginning of the list item, containing the index of the list item.
*/
$( 'li' ).each(function( index, elem ) {
  // this: the current, raw DOM element
  // index: the current element's index in the selection
  // elem: the current, raw DOM element (same as this)
  $( elem ).prepend( '<b>' + index + ': </b>' );
});
/*
Inside the function that we pass to .each(), we have access to the current raw DOM element in two ways: as this and as elem. 
*/

$(".mi-selector").each(function(i, el){
  $(el).addClass('fru');
});

/*
Add class sequentially
*/
var addClassToEl = function($el) {
  $el.addClass('animated fadeInDown');
};

$('.row-assets .asset').each(function(i, el) {
  setTimeout(function() {
    addClassToEl($(el))
  }, i++ * 500);
});