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

239
Vistas
Javascript: Clicking an injected button doesn't work

when I inject some code the onclick doesn't work here is the code I use.

 
document.getElementById('headbar').onclick = function() {
var list_div_preleva =  document.getElementsByClassName('preleva_dati').length; 
var list = document.getElementsByClassName('box_commenti');

for(var i = list_div_preleva; i < list.length; i++) {
var parentDiv = list[i].parentNode.id;
var id_post = document.getElementById(parentDiv);
var creaelementodiv = document.createElement("button");
creaelementodiv.className = "preleva_dati";
creaelementodiv.setAttribute("id", "blocco_dati");
creaelementodiv.innerHTML = "<li>preleva il post</li>";
id_post.appendChild(creaelementodiv);
}
};
<div id="headbar">div</div>

when the code is inside, onclick doesn't work

 

<!-- begin snippet: js hide: false console: true babel: false -->

document.getElementById('blocco_dati').onclick = function() {
alert(11);
};
can the problem be solved?

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

document.getElementById('headbar').onclick = function() {




var id_post = document.getElementById("headbar2");
var creaelementodiv = document.createElement("button");
creaelementodiv.className = "preleva_dati";
creaelementodiv.setAttribute("id", "blocco_dati");
creaelementodiv.innerHTML = "preleva il post";
creaelementodiv.setAttribute("onclick", "doSomething();");
id_post.appendChild(creaelementodiv);
};


function doSomething() {
alert(11);
};
<div id="headbar">div</div>

<div id="headbar2"></div>

You can set one more attribute while injecting element

for ex:

creaelementodiv.setAttribute("onclick", "doSomething();");

and then you can call that function like

function doSomething() {
alert(11);
};
about 4 years ago · Santiago Gelvez 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