Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Blake Baxendell
16,344 PointsGetting console error for addEventListener being null
Code looks the same. My js:
const myHeading = document.getElementById('myHeading');
myHeading.addEventListener('click', () => { myHeading.style.color = 'red'; });
Normally this is called if you don't have the Id in the HTML, but my h1 has the id of myHeading.
1 Answer
Umesh Ravji
42,386 PointsIf the console is warning you: Cannot read property 'addEventListener' of null, that would mean an element wasn't returned from your document.getElementById() function.
Are you running the JavaScript after the contents of your page (added at the bottom)? Your code looks fine, so I'm assuming that you could be including it in the header, rather than after the contents.
Blake Baxendell
16,344 PointsBlake Baxendell
16,344 PointsThat was it, my bad. I must of missed that step in the video. Thanks.