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

106
Visualizações
Javascript adding a new event listener on local object

I have a script below, where I dynamically create an Element Node of type 'li'. This addMovieHandler() is called whenever the user clicks on a button.

Additionally, the newly addded list item should be deleted on 'click'.

So, what I experimented is I added an event Listener on the local object newLi and also passed that to the deleteMovieHandler() function.

And It Works. I added several list items and when I clicked any, it was gone from the screen.

const movieList = document.getElementById('movie-list');

const deleteMovieHandler = (newLi) => {
    newLi.style.display = 'none';
}

const addMovieHandler = (image) => {
    
    const newLi = document.createElement('li');
    newLi.className = 'movie-element';

    newLi.innerHTML = `
    <div class = "movie-element__image">
        <img src="${image}"></img>
    </div> `;

    newLi.addEventListener('click', deleteMovieHandler.bind(this, newLi));

    movieList.appendChild(newLi);
}

I get it that this is bad practice to just hide the element, which becomes a resource wastage.

But the center of interest is that there was a separate event listener on every list item, even though that local object should have been lost after function termination.

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