Saturday, 17 August 2013

JavaScript find li index within ul

JavaScript find li index within ul

I'm trying to find the index of my list item by id in Javascript. For
example I have list of 5 items and given an element I want to figure out
which position it is in the list. Below is the code that I hope to build
on.
It's using an onclick handler to find the element, which is working, I
then just need to figure out the position of the element in the list
'squareList' in some way.
Thanks for the help!
window.onload=function(){
function getEventTarget(e){
var e=e || window.event;
return e.target || e.srcElement;
}
function selectFunction(e){
var target=getEventTarget(e);
alert(target.id);
}
var squareList=document.getElementById('squareList');
squareList.onclick=function(e){
selectFunction(e);
}
}

No comments:

Post a Comment