The usual trick is to find the LI that the click came from, the get its parent UL and loop over the LI children to find the subject LI:
<ul id="ul0">
<li><span>first</span>
<li><span>second</span>
<li><span>third</span>
</ul>
<script>
document.getElementById('ul0').addEventListener('click', showIndex, false);
function showIndex(event) {
var index;
var element = event.target;
if (element.tagName.toLowerCase() != 'li') {
element = upTo('li', element);
}
if (element) {
index = getIndex(element);
}
console.log(index);
return index;
}
// Get the index of an LI in a UL
// Index is zero based
function getIndex(li) {
var ul = li.parentNode;
var node;
var nodes = ul.childNodes;
// Traverse child nodes, counting LIs until element found
for (var i=0, j=0, iLen=nodes.length; i<iLen; i++) {
node = nodes[i]
if (node.tagName && node.tagName.toLowerCase() == 'li') {
if (node == li) {
return j;
}
j++;
}
}
return null;
}
// Start from root and find ancestor with tagName
// Returns undefined if element not found
function upTo(tagName, root) {
if (!root) return;
tagName = tagName.toLowerCase();
while (root.parentNode) {
root = root.parentNode;
if (root.tagName && root.tagName.toLowerCase() == tagName) {
return root;
}
}
}
</script>
This could also handle LIs within an OL by seeing if the LI's parent is an OL and if so, just return li.value
.