Iterators

By now you should have realized that the Karmagination is basically an object with a collections of elements. Sometimes it is necessary to iterate though all the elements and do something with them.

each( callback )

Iterates and firing a callback through each and every element. Two are arguments are passed to the callback function, the current element and the ith iteration. Returns the Karmagination object. This method is chainable.

<div>A</div>
<div>B</div>
<div>C</div>
Karma('div').each(function(el, i){
 el.style.color = 'red';
 el.innerHTML = 'This is div ' + i;
});

map( callback )

Iterates through each and every element and map them into data that's useful. Returns an array.

<div>14000</div>
<div>94538</div>
<div>56421</div>
var allZip = Karma('div').map(function(el, i){
 return 'Zip number ' + (i+1) + 'is ' + el.innerHTML;
});
 
alert(allZip); // [ 'Zip number 1 is 14000', 'Zip number 2 is 94538', 'Zip number 3 is 56421' ]

grep( callback )

Goes through all the elements and grabs those elements that meet a certain criteria. Returns the Karmagination object. This method is chainable.

<div>100</div>
<div>1000</div>
<div>10000</div>
var metCriteria = Karma('div').grep(function(el, i){
  return parseInt(el.innerHTML) > 500;
});
 
alert(metCriteria.get()); // [ <div>1000</div>, <div>10000</div> ]