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

125
Views
¿Cómo asigno el valor de button.setAttribute('name', 'value') una variable de JavaScript?

Primero, aquí está el código javascript.

 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)');

y este es el resultado HTML

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

¿Cómo hago para que HTML/javascript reconozca nameId como su valor?

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

0

No necesita usar ${...} para pasar el valor de var a la función setAttribute. Use solo el nombre de la variable. Como eso:

 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

Y resolví mi propia pregunta... use lo siguiente en el código...

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

Este es un momento Facepalm...

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!