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

110
Vistas
Unable to add click event to button that isn't created until the object is created

I'm trying to select to element class 'remove-book' that isn't created until after display books is ran..how do I go about selecting this remove button AFTER it's created? Here is link to the github https://github.com/Cluelesshint/library the 'displayBooks()' function is what creates that class..please help!

buttons.forEach((button) => {
  if (button.className === 'new-book') {
    button.addEventListener('click', function () {
      const input = document.querySelector('.book-input');
      openInput(input);
    });

  }
  else if (button.className === 'add-input') {
    button.addEventListener('click', addABook);
  }
  else if (button.className === 'remove-book') {
    button.addEventListener('click', doThis);
  }
  console.table(buttons);
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try to use event delegation. The idea is simple: instead of listening events on the target element, listening events on it's ancestor elements. If an event is happening, the browser would work like the following.

  1. The browser checks to see if the direct parent of the element selected has an onclick event handler registered on it for the bubbling phase, and runs it if so.
  2. Then it moves on to the next immediate ancestor element and does the same thing, then the next one, and so on until it reaches the element.

So, when the element is clicked, the event is bubble up to its ancestor element. It doesn't care when and how the element is created.

Refactor your code with event delegation would be something like the following.


document.addEventListener('click', (event) => {
  if (event.target.tagName == 'BUTTON') { // make sure the target is a button element
    const button = event.target; // this is the button clicked.
    const classNames = event.target.classList
    if (classNames.contains('new-book')) {
      const input = document.querySelector('.book-input');
      openInput(input);
    } else if (classNames.contains('add-input')) {
      button.addEventListener('click', addABook);
    } else if (classNames.contains('remove-book')) {
      button.addEventListener('click', doThis);
    }
  }
})

note: I'm not sure why you add another event listener when a button is clicked. This is just a refactor in a perspective of event delegation

about 4 years ago · Juan Pablo Isaza Denunciar
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