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

107
Views
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
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!