Return to Snippet

Revision: 12360
at March 12, 2009 05:07 by 1man

Initial Code
//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;


Initial URL

Initial Description
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.

Initial Title
Find selected elements index

Initial Tags

Initial Language