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

120
Vistas
AddEventListener is not getting into his callback function when click

I have a dynamic button that supposed to remove his parent element from the DOM. Because its dynamic, i warpped it with DOMContentLoaded and added for each button the EventListener with 'click'. Somehow, i dont get into the callback function (deleteTodo) when click. I'll happy to know what im doing wrong. Thanks :)

document.addEventListener('DOMContentLoaded', (event) => {


let deleteBtn = document.getElementsByClassName('delete-btn');
for( let btn of deleteBtn)
    btn.addEventListener('click', deleteTodo);

});

// Delete element
const deleteTodo = () => {

console.log("inside function"); // I DONT GET INTO THIS LINE WHEN CLICK

}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can't call a function assigned to a variable that hasn't been initialised. Use a function declaration (they're automatically hoisted) rather than a function expression so you can ensure that when your code tries to add the function to the listener it's actually available. And, as you can see from the example, it doesn't matter where you add the declaration.

function addThings() {
  const html = [];
  for (let i = 0; i < 5; i++) {
    html.push(`<div>${i}&nbsp;<button class="delete-btn">Delete</button></div>`)
  }
  return html.join('');
}

document.body.innerHTML = addThings();

const buttons = document.querySelectorAll('.delete-btn');

buttons.forEach(button => {
  button.addEventListener('click', deleteTodo, false);
});

function deleteTodo() {
  this.parentNode.remove();
}

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