views:

29

answers:

2

Is there a way through which I can get the index of list Items on click of li using javascript/jquery

<ul><li>item1</li><li>item2</li><li>item3</li></ul>
+1  A: 
$('ul li').click(function(){ alert($(this).index()); });
Thomas Clayson
A: 

Use the onclick event to call the function getIndex (no jQuery needed):

onClick = "getIndex(this);"

function getIndex(node){
  childs = this.parentNode.childNodes;
  index = 0;
  for (i=0,i < childs.length;i++)(
    index = i;
    if (node == childs[i]) break;
  }
  return index;
}

Index will start beginning with 0! To let it start with 1 : return index+1

Thariama