0

I'm trying to get some images to work with lightbox but I do not want all images on that site to react to the eventlistener.

This is how my images are declared:

<img class="thumbnail" id="imga" src="~/lib/images/IC/Bild1,5_Pirellival.png"/>

This is what I tried but it is not working:

const images = document.getElementsByClassName('thumbnail')

I do not want to use querySelectorAll('img') because this would mess up my other images on the same page.

Any advice?

Amila Senadheera
  • 12,229
  • 15
  • 27
  • 43
ottdevott
  • 25
  • 4
  • Why not use `querySelectorAll('.thumbnail')`? – Heretic Monkey Feb 04 '22 at 13:44
  • You haven't defined what you mean by "not working," but `document.getElementsByClassName` isn't broken. If there are elements in the document with that class, they **will** be in the `HTMLCollection` it returns. So presumably, they aren't in the document (yet?). – T.J. Crowder Feb 04 '22 at 13:45
  • @HereticMonkey - They could, but if `getElementsByClassName` isn't finding them, `querySelectorAll` won't either. :-) Though there is the distinction between the live collection from the former and the snapshot from the latter. – T.J. Crowder Feb 04 '22 at 13:45
  • 1
    @T.J.Crowder Yeah, I've been around this block a few times too. Just trying to gently introduce the concept of using class selectors with `querySelectorAll`. There's really not enough in the question to indicate which collection type they need. – Heretic Monkey Feb 04 '22 at 13:50

0 Answers0