Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
Callback function on addEventListener

so in this challenge, I have to change the element's color on mouse entering it as well as mouse leaving.

This is the website for the challenge.

https://www.jschallenger.com/javascript-dom-exercises/events-and-user-interactions/cursor-enter-leave-event

Below lines of code are presented:

const element = document.querySelector('#element');

const toggleColor = (isEntering) => {
  element.style.background = isEntering ? 'orange' : 'black';
};

And I came up with below codes:

element.addEventListener('mouseenter', toggleColor(true));
element.addEventListener('mouseleave', toggleColor(false));

But it doesn't work and the correct answer is as below:

element.addEventListener('mouseenter', () => toggleColor(true));
element.addEventListener('mouseleave', () => toggleColor(false));

Here, why can't I just put toggleColor(param) directly into addEventListener's 2nd parameter? Why do I have to put arrow function to call the toggleColor function?

It would be much appreciated if anyone could explain it.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!