Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

326
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda