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.
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);
}
}
squareList
id for the<ul>
? Do your<li>
elements have a shared class? Are there any nested lists? The answers to all these would be given with a jsFiddle. – Northumbriavalue
would make the HTML invalid.data-value
, though...maybe. – Snorkel