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 trialVishesh Singh
4,332 PointsUnexpected behavior of addEventListener() method.
I have tried to call a function in the addEventListener() method, as the second argument may be a javascript function ( I had checked details about the addEventListener() method on MDN (https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener , please check the detail about the parameter "listener")
My code is as below:
// selecting the myHeading element
const myHeading = document.getElementById( "myHeading" );
// changing the color of the myHeading element
function eventClick( ) {
myHeading.style.color = "red";
}
myHeading.addEventListener( "click", eventClick() );
Now, the problem is when I am previewing the HTML page, the red color is already applied to the heading element on the page load. Why such a strange behavior?
Above behavior is same if I convert the function declaration to a function expression and call it in the addEventListener() method.
2 Answers
Steven Parker
231,271 PointsThe code above calls the function (which makes the heading red) instead of passing it as a callback. To do the latter, omit the parentheses after the function name:
myHeading.addEventListener( "click", eventClick );
Vishesh Singh
4,332 PointsThank you Steven.
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsAakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsHey Steven . I didn't got that. Can you please elaborate that.
I understood that if i used paranthesis , then heading will be red. But removing the parenthesis works. How?
Steven Parker
231,271 PointsSteven Parker
231,271 PointsWithout parentheses, the function is passed as a callback to the listener, and it will be called later by the listener when the event occurs.
Putting parentheses after the name calls the function immediately and passes whatever it returns as the callback parameter (which is not likely to be an appropriate parameter for the listener).
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsAakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsI am getting what you want to say . But I have a doubt as
eventClick
is a function not a variable. How is it will be called when the event occurs. Where does it mentioned in the code thateventClick
will becomeeventClick()
when the event happens?Steven Parker
231,271 PointsSteven Parker
231,271 PointsFor more details, see the MDN page on addEventListener.