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

284
Vistas
Problems with document.addEventListener vs element.addEventListener

I was working on some code I wrote and I had this problem where an eventListener I added to a dynamically created element, but it does not "fire" aka work.

var eleButton = document.createElement("button");
eleButton.innerText="Post";
eleButton.type="submit";
...
eleButtonContainer.appendChild(eleButton);

And the eventlistener was a basic "click"

eleButton.addEventListener("click", function() {
    console.log("Hello World");
});

That did not work, so i searched around and could not find any answers. I finally found a work around of using document and checking whether or not that element was check or not. So my resulting code became this:

var eleButton = document.createElement("button");
eleButton.innerText="Post";
eleButton.type="submit";
eleButton.id = "usrResponseSubmit";

console.log(eleButton);
document.body.addEventListener('click',function(){
   if(event.target.id == "usrResponseSubmit"){
       console.log("ok now it works");
   }
});

This works fine, but it still makes me question about the code atop. I willing to take the "if it werks it works" route, but I would also still like to use the eleButton.addEventListener route because that was what I always used.

So my two questions are:

  1. is using document.addEventListener bad?
  2. why does eleButton.addEventListner not work?

Before you say, but it works on my end, my code is kinda crap and long. Essentially, I have this eleButton nested in a container that is nested in a container ... (x~10-20). I don't think this is necessarily the problem. But I also have some other eventListeners that "may" overlay with eleButton. I'm not too sure that's the case as i've been trying and testing everything I can.

I have also pulled the eleButton and appended it to an existing object and the event did fire. Can someone give me a run down of when events do not fire?

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

0

Nothing is wrong with your code, maybe you just misspelled something. here i made a codepen example you can check.

html:
<div id="btnCon"></div>

...

JS:
const eleButtonContainer = document.querySelector('#btnCon')

var eleButton = document.createElement("button");
eleButton.innerText="Post";
eleButton.type="submit";

eleButtonContainer.appendChild(eleButton);

eleButton.addEventListener("click", function() {
    console.log("Hello World");
});

the example on codepen

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