0

I am just trying to set up a simple addEventListener but it is giving me type error.

I've tried switching the order of my js but nothing seems to work.

// my script

const serviceContainer = document.getElementsByClassName("serviceImg-container");

serviceContainer.addEventListener('click', function(){
  alert("Hellooo worlddd!!");
});
Christian Carrillo
  • 2,751
  • 2
  • 9
  • 15
  • `getElementsByClassName` will target a nodeList with the class ´serviceImg-container`, then u should iterate each item of the nodeList with his listener. – Christian Carrillo Aug 16 '19 at 01:08

1 Answers1

1

I prefer working with id's makes it easier because there can only be one Id.

<div id='serviceImg-container'><img src='someimage.com'/></div>
document.querySelector('#serviceImg-container').addEventListener('click', function(e) {
    alert('hello world!');
}
tytyf
  • 342
  • 1
  • 2
  • 11
  • 1
    It worked dude. Thank you!! I wanted to put the ID in a variable and then use the event listener method on that ID. But either way, you helped a bunch. Thanks! – Matthew J. Joseph Aug 16 '19 at 01:47