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

148
Views
How to combine several getElementsByName into one operation?

I have the following javascript and I need to combine them into one operation if possible.

    document.getElementsByName("last_name")[0].required = true;
    document.getElementsByName("company_name")[0].required = true;
    document.getElementsByName("companyIndustry")[0].required = true;
    document.getElementsByName("company_address")[0].required = true;
    document.getElementsByName("email")[0].required = true;
    document.getElementsByName("phone")[0].required = true;
    document.getElementsByName("products")[0].required = true;
    document.getElementsByName("comments")[0].required = true;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using an array of classes with a loop can reduce redundant code:

const classes = ["last_name", "company_name", ...];

for (const className of classes) {
    document.getElementsByClassName(className)[0].required = true;
}

And like @nina-scholz suggested you might want to use ids instead:

const ids = ["last_name", "company_name", ...];

for (const id of ids) {
    document.getElementById(id).required = true;
}
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!