-2

Here is the piece of HTML code. I captured the buttonElements using document.getElementsByClassName('buttonEl') and added a eventListener. And I want the index of the particular button when it is clicked.

<div class="action">
  <div class="buttonEl"></div>
  <div class="buttonEl"></div>
  <div class="buttonEl"></div>
</div>
shan
  • 1

2 Answers2

-1

This would be one way of doing it...

document.querySelectorAll will get an array of elements (buttons) based on the class selector.

We then loop over this array using forEach - button is the current element we are iterating over while index is the index of that element in the array.

Finally, while looping, we attach a "click" event listener to each button and when that event is fired, we console.log the index from earlier.

const buttons = document.querySelectorAll('.buttonEl')
buttons.forEach(function(button, index){
  button.addEventListener('click', function(){
    console.log(index)
  })
})
.action{
  display: flex;
  justify-content: space-around;
}

.buttonEl {
  background-color: #CCC;
  padding: 10px;
  border-radius: 5px;
}
<div class="action">
  <div class="buttonEl">Foo</div>
  <div class="buttonEl">Bar</div>
  <div class="buttonEl">Baz</div>
</div>
Simon K
  • 1,469
  • 1
  • 7
  • 9
-2

I believe you're looking for the indexOf array method.

// Arrays have the indexOf method, but NodeLists do not
const buttons = Array.from(document.querySelectorAll('.buttonEl'));
const buttonParent = document.querySelector('.action');
buttonParent.addEventListener('click', (evt) => {
  let target = evt.target;
  let index = buttons.indexOf(target);
  console.log(index);
});
Leshawn Rice
  • 450
  • 3
  • 11
  • 4
    Putting the question title into google gives the duplicate as first result. Always check for duplicates. Easy questions are always duplicate. Without exception. This website is not a casual chat forum. The other person and I didn't downvote this because the code doesn't work, we downvoted because this answer is literally "not useful", as the tooltip of the downvote button states. – ChrisG Mar 10 '22 at 19:53
  • How is a valid answer to the question "not useful"? I understand it might encourage more low-effort questions if duplicates are answered, but I don't see how a valid answer could be considered "not useful" – Leshawn Rice Mar 10 '22 at 19:57
  • 2
    Stack overflow is supposed to be an exhaustive FAQ of programming problems. This means that every question and answer are supposed to contribute to this FAQ, and duplicates are supposed to be removed. Given this context, posting an answer to a duplicate question is absolutely "not useful". And *anything* that encourages people to engage in low-effort behavior is much worse than not useful, it's actively harmful to the quality and purpose of this website. Just because the management no longer tells newbies the rules doesn't mean we should push SO over the cliff edge deliberately. – ChrisG Mar 10 '22 at 20:05
  • So the onus for researching a question not only falls on the asker, but the person answering as well? For a question like this it's obvious it's probably a duplicate, but for a question a bit more esoteric, are users expected to google every question before answering just to make sure they don't answer a duplicate? I understand closing duplicate questions, but discouraging users from answering questions because they might be a duplicate seems unproductive. – Leshawn Rice Mar 10 '22 at 20:10
  • 2
    You're absolutely supposed to check for it being a duplicate first. The less needs to be removed / cleaned up, the better. If you do come across an esoteric question, feel free to not spend half an hour searching for a duplicate first, obviously. This is not about discouraging people from participating, it's about keeping this site a useful resource. Posting a question should be a last resort, not the first thing you do once you got stuck for five minutes. Answering dupes encourages low quality questions and gives the wrong impression to newbies about how SO is supposed to work. – ChrisG Mar 10 '22 at 20:16
  • That's fair enough – Leshawn Rice Mar 10 '22 at 20:24