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

88
Views
loop through html inputs that have an ID

I'm struggling here with some javascript code I need. I have an html document with more than 300 inputs (type text). All inputs have an id, some inputs also have a name (similar to the id). I want to loop through all input tags that have an ID but do not have a name and for each one add a name attribute identical to the ID.

I think it's simple and I know how to find or loop through elements with specific ID but there is something I'm missing and cant figure it out.

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

0

You can use querySelectorAll with the attribute selector [attr], loop through and check if it's got a name, if not copy the ID to the name.

eg.

for (const el of document.querySelectorAll('input[id]')) {
  if (!el.getAttribute('name')) {
    el.setAttribute('name', el.getAttribute('id'));
  }
}

console.log(document.querySelector('form').innerHTML);
<form>
  <input id="bob"/>
  <input id="john"/>
  <input name="gotname"/>
</form>

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!