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

216
Views
Javascript dynamic button id

I have dynamically made a button using Javascript, now i need to give that button an ID, i have tried with

buttonElementBlokkeer.setAttribute("id", "buttonBlokkeer");

& 

buttonElementBlokkeer.id = "buttonBlokkeer"

Both is not working. The buttons are nested in a section that already has an id, could this be a reason why it is not working?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

HTML

<div id="divId"></div>

JS

for (i = 1; i <= 3; i++) {
    var btn = document.createElement("BUTTON");// create       button using java script
    btn.className = "btnsize";
    btn.id = "btnid"+ i;
    var txt = document.createTextNode("Buttons"+i);//creat text on button
    btn.appendChild(txt);//attached text on button
    document.getElementById("divId").appendChild(btn);//atache button with text in div 
}
about 4 years ago · Juan Pablo Isaza Report

0

Try this:

let button = document.createElement('input');
button.setAttribute('type', 'BUTTON');
button.setAttribute('ID', 'buttonBlokkeer');
button.setAttribute('value', 'BUTTON');
button.setAttribute('onclick', 'test()');
document.body.appendChild(button);


function test() {
  console.log("BUTTON !");
  alert("BUTTON !");
}

https://jsfiddle.net/onw2esyg/

about 4 years ago · Juan Pablo Isaza Report

0

This is working for me. You can use getElementsByClassName() function if you want to access by class name.

<button id="changeId">Button1</button>
<button onclick="update()">Update</button>
    
<script>
function update() {
   document.getElementById("changeId").id = "IdUpdated";
}
</script>
about 4 years ago · Juan Pablo Isaza 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!