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

283
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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