====== 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.
A
B
C
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.
14000
94538
56421
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.
100
1000
10000
var metCriteria = Karma('div').grep(function(el, i){ return parseInt(el.innerHTML) > 500; }); alert(metCriteria.get()); // [
1000
,
10000
]