is there any way to get index of clicked div in parent div?
Here's an example:
<div class="parent">
<div id="1" class="child"></div>
<div id="2" class="child"></div>
<div id="3" class="child"></div>
<div id="4" class="child"></div>
</div>
<script>
var parent = document.getElementsByClassName("parent");
var child = document.getElementsByClassName("child");
for(var i=0; i < parent.length; i++){
parent[i].addEventListener("click", function(e){
console.log(e.target)
console.log(e.target.parentNode)
});
}
</script>
What I mean is, is there any way to get index of clicked div? For example, if I would click div with id "1", it would print "0" in console since its index is 0. If I would click div with id "2", it would print "1" in console since its index is 1, and so on.
Is there any way to do this? (without using attributes)