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

126
Views
How do I assign value of button.setAttribute('name', 'value') a javascript variable

First, here is the javascript code.

    const nameId = calledVariable.value // This variable is an input from a form

    // Create the button
    var payButton = document.createElement('input');
    payButton.setAttribute('type', 'button');
    payButton.setAttribute('value', 'UNPAID');
    payButton.setAttribute('class', 'unpaidButton');
    payButton.setAttribute('id', nameId); // This needs to be text of input
    payButton.setAttribute('onClick', 'updateButton(this.id)');

and this is the HTML result

<input type="button" value="UNPAID" class="unpaidButton" id="" onclick="updateButton(this.id)">

How do I get the HTML/javascript to recognize nameId as it's value?

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

0

You don't need to use ${...} to pass the var value to the setAttribute function. Use only the variable name. Like that:

const nameId = 123;
const payButton = document.querySelector('button');
console.log('before', payButton)
payButton.setAttribute('id', nameId);

const check = document.querySelector('button');
console.log('after',check)
<button>button</button>

about 4 years ago · Juan Pablo Isaza Report

0

And I solved my own question... use the following in the code...

payButton.setAttribute('id', `${nameId}`);

This is a Facepalm moment...

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!