I want to get the index value of the div which has been clicked. I have 6 divs. When I click on them using e.target
from the whole document, I want to get the index position of the clicked one from the 6 divs
const myall = Array.from(document.querySelectorAll('.same'));
const thum_container = Array.from(document.querySelectorAll('.item-thumb'));
document.addEventListener('click', (e) => {
if(e.target.classList.contains('item-thumb')){
alert('lets see ' + thum_container.indexOf(e.target));
}
});
<div id="modal-product-thumb-item" style="border-radius: 12px; height: 100%; border: 1px solid #e8e8e1; overflow: hidden; background: #f1f1f1;">
<a class="item-thumb">
<h2>
one
</h2>
</a>
</div>
<div style="border-radius: 12px; height: 100%; border: 1px solid #e8e8e1; overflow: hidden; background: #f1f1f1;">
<a class="item-thumb">
<h2>
two
</h2>
</a>
</div>
<div style="border-radius: 12px; height: 100%; border: 1px solid #e8e8e1; overflow: hidden; background: #f1f1f1;">
<a class="item-thumb">
<h2>
three
</h2>
</a>
</div>
<div style="border-radius: 12px; height: 100%; border: 1px solid #e8e8e1; overflow: hidden; background: #f1f1f1;">
<a class="item-thumb">
<h2>
foure
</h2>
</a>
</div>
<div style="border-radius: 12px; height: 100%; border: 1px solid #e8e8e1; overflow: hidden; background: #f1f1f1;">
<a class="item-thumb">
<h2>
five
</h2>
</a>
</div>
<div style="border-radius: 12px; height: 100%; border: 1px solid #e8e8e1; overflow: hidden; background: #f1f1f1;">
<a class="item-thumb">
<h2>
six
</h2>
</a>
</div>
I want to get the index value of the div which has been clicked. I have 6 divs, when I click on them using e.target from the whole document, I want to get the index position of the clicked one from the 6 divs
const myall = document.querySelectorAll('.container');
document.addEventListener('click', (e) => {
if(e.target.classList.contains('same')){
myall.forEach((eachitem, eachindex) => {
eachitem.addEventListener('click', () => {
alert('lets see ' + eachindex);
});
});
}
});
<div class="container">
<div class="same">1</div>
<div class="same">2</div>
<div class="same">3</div>
<div class="same">4</div>
<div class="same">5</div>
<div class="same">6</div>
</div>