/ Published in: jQuery

Never used the index() method in jQuery but could be useful to know. Find a selected elements index when you click on it. Note the $('ul#mylist') inside the index method, gives the search some context of where to look for .selected, saves looking through all the DOM.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//jQuery only var selected = $('ul#mylist li').index( $('.selected',$('ul#mylist')) ); //Dirty Javascript / jQuery way var selected = 0; // Iterate through item in the list. If we find the selected item, return false to break out of the loop $(‘ul#mylist li’).each(function(index){ if ($(this).hasClass(‘selected’)){ selected = index; return false; } });
URL: http://crad.tumblr.com/post/85523431/jquery-tip-how-to-break-out-of-each
Comments
