Skip to content
Advertisement

Getting a “Uncaught TypeError: Cannot read property ‘addEventListener’ of null”

I’m practicing my vanilla JS and trying to create dynamic elements. I came across some interesting behavior. I am simply creating a button, click it and then have it render onto the DOM. But then I want to create another event that mouses over the h1 elements and changes the color, however I get a “Uncaught TypeError: Cannot read property ‘addEventListener’ of null”. Why is this showing up as null if there is a h1 on the DOM and why does it now say cannot read property “addEventListener” of null?

JavaScript
JavaScript

Advertisement

Answer

Add your h1 event listener inside the function since there’s no h1 on load.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement