====== Filters ======
Sometimes it is necessary to filter out some elements.
===== [ n ] =====
Gets the nth element that are current contained in that instance of Karmagination object. The returned element will lose access to Karmagination's method.
One
Two
Three
alert(Karma('div')[0].innerHTML); // One
alert(Karma('div')[1].innerHTML); // Two
alert(Karma('div')[2].innerHTML); // Three
===== length =====
Represents the number of elements that are current contained in that instance of Karmagination object.
One
Two
Three
alert(Karma('div').length); // 3
===== get( ) =====
Returns all the elements contained in the Karmagination object as an array. In other words, this method strips the Karmagination object wrapper and returns a true array containing all of the elements.
A
B
C
alert (Karma('div') instanceof Array); // false
alert (Karma('div').length); // 3
alert (Karma('div').get() instanceof Array); // true
alert (Karma('div').get().length); // 3
===== index( selector ) =====
Returns the index (starting from 0) of the first element that matches the first element returned by the selector. If there are no matches, -1 is returned
index 0
index 1
index 2
index 3
alert(Karma('div').index('#find')); // 0
alert(Karma('div').index('#wanted')); // 2
alert(Karma('div').index('#notfound')); // -1
===== index( element ) =====
Returns the index (starting from 0) of the first element that matches the element. If there are no matches, -1 is returned
LOL
index 0
index 1
index 2
index 3
alert(Karma('div').index(document.getElementById('find')); // 0
alert(Karma('div').index(document.getElementById('wanted')); // 2
alert(Karma('div').index(document.getElementById('notfound')); // -1 because notfound is
===== eq( n ) =====
Gets the nth element and return the object wrapped in Karmagination. This method is chainable.
A
B
C
alert(Karma('div').length); // 3
alert(Karma('div').eq(0).length); // 1
alert(Karma('div').eq(0)[0].innerHTML); // A
alert(Karma('div').eq(0).end().length); // 3
===== slice( start, [end] ) =====
Gets the elements from the start index till end index. If end is not specified, then the method will get till the last element. Returns the elements wrapped in Karmagination. This method is chainable.
One
Two
Three
alert(Karma('div').length); // 3, elements contained are [ One
, Two
, Three
]
alert(Karma('div').slice(1).length); // 2, elements contained are [ Two
, Three
]
alert(Karma('div').slice(1).end().length); // 3, elements contained are [ One
, Two
, Three
]
===== filter( selector ) =====
Removes all element that does not match the selector
a
b
c
alert(Karma('*').filter('span').length); // 2
alert(Karma('*').filter('span').get()); // [ a
, b
]
===== 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
]
===== is( selector ) =====
Returns true is one of the elements matches the expression
===== not( selector ) =====
Removes elements that do not match the selector