Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

336
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda