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
Byron Farrell
5,700 PointsJavaScript event callback
Where is the "event" object coming from in this code
DOM_Object.addEventListener('click', event => {
//Do stuff
});
How is the event parameter getting the event object of the DOM_Object?
1 Answer
Michael Hulet
47,913 PointsJavaScript calls that function whenever DOM_Object is clicked. Sometimes, though, we'll want to know information about that event/click, so JavaScript also creates an object with potentially useful information about it and passes it to our handler function. That object is event in your code. Basically, JavaScript internally does this whenever a click happens on DOM_Object:
const clickDescription = { /* Tons of information about the click here */ };
yourClickHandlerFunction(clickDescription);
Later on in the code you posted, event is equal to clickDescription, which is an object describing the click event