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 trialMaxim Melnic
4,178 PointsHi, why addEventListener don't work without function?
Hi, why addEventListener don't work without function?
for example:
const myHeading = document.getElementById("myHeading"); myHeading.addEventListener('click', 'myHeading.style.color = "red"');
I see an errore in console "Argument 2 of EventTarget.addEventListener is not an object"
In what sense?
1 Answer
kevin hudson
Courses Plus Student 11,987 PointsYou need to put an anonymous function in so that the addEventListener method does not fire off immediately when the page loads. When you click then the new function executes what needs to be done.
myHeading.addEventListener('click', 'myHeading.style.color = "red"');
Will immediately change to red.
Antonio Ascue Avalos
3,023 PointsDoesn't work. The color remains black despite clicking on the title. Why?, I am using Chrome.
Otec Perez Glass
7,678 PointsOtec Perez Glass
7,678 PointsHi Max Mel Hope you are doing good, the only issue is that you forgot to call the anonymous function or the call back function.