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