Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!