JavaScript JavaScript and the DOM Getting a Handle on the DOM A Simple Example

Peter Ajuzie
Peter Ajuzie
Full Stack JavaScript Techdegree Student 2,719 Points

Which "myHeading"?

Please, which myHeading is the addEventListener manipulating? Is it the ID or the constant?

1 Answer

Aayush Mitra
Aayush Mitra
24,360 Points

Hi Peter!

It is the constant. In javascript you cannot select an element by its id without using the document.getElementById() method. To add an event listener for that id's element, he needs to store it in a variable, or a constant:

const myHeading = //make variable
document.getElementById("myHeading");// getting element and storing it in that variable.
myHeading.addEventListeneter(); //Making event listener for the VARIABLE myHeading which contaings the element's ID.

Hope this helps!

Thanks!   :)