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

329
Views
addEventListener is not working? It didn't perform only when the event happened

I wanted to add a new class(".when-hover") to some HTML elements(showcases) when I hovered on them. So I wrote JS like this:

const beforeHover = document.getElementsByClassName("before-hover");
const showcases = document.getElementsByClassName("project-tile");

for (let showcase of showcases) {
    showcase.addEventListener("mouseover", addClass("when-hover"));
}

function addClass(newClass) {
    for (let el of beforeHover) {
        el.classList.add(newClass);
    }
}

But when I tested it, the new class (".when-hover") was added to the elements from the moment I opened the page. But I wanted the new class to be added only when hovering (mouseover). What went wrong with my code?

Here's my HTML code:

<a href="#">
                <div class="project-tile">
                    <div class="project-showcase">
                        <img class="project-img"
                            src="https://cdn.freecodecamp.org/testable-projects-fcc/images/tribute.jpg"
                            alt="Overview image of project name Tribute Page">
                    </div>
                    <div class="project-name">
                        <p><span class="before-hover">&#60;</span>Tribute Page<span
                                class="before-hover">&#47;&#62;</span></p>
                    </div>
                </div>
 </a>
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!