0

I am trying to loop through the items array and whichever element has been clicked, then apply functionality to it.

For example:

var items = getElementsByClassName("media-item");

addEventListener("click", function() {

  for(var i = 0; i < items.length; i++) {
    if(items[i].onclick) {
      items[i].classList.toggle("show-media-item");
      items[i].getElementsByTagName("figcaption")[0].classList.toggle("show-caption");
    }
  }

}
Hezerac
  • 334
  • 1
  • 5
  • 20
  • That's not how you do that... – Niet the Dark Absol Sep 26 '16 at 18:05
  • 1
    You should [read the addEventListener docs](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) - event handlers are added to _one_ element. You should define one function to do the `classList` toggling then iterate items and attach the event handler to each: `items[i].addEventListener('click', classToggler);` – Rob M. Sep 26 '16 at 18:11

0 Answers0