I am trying to get the index of the element clicked but for some reason I always get -1. I tried putting the parent as first parameter and the ancestor as second and vice versa but it doesnt work. What am I doing wrong?
HTML Example:
<div>One</div>
<div>Two</div>
JS Example:
function elementNr(target, parent) {
console.log(Array.prototype.indexOf.call(parent, target));
}
document.body.addEventListener("click", function(e) {
if(e.target != e.currentTarget) {
elementNr(e.target, this)
}
});
I got the idea from this post: problems with Array.prototype.indexOf.call
I wanted to see if this works with event delegation like the example above.
Goal: Using event delegation and Array.prototype I want to get the index of the element clicked.