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

140
Views
How do I add multiple atrributes to an element in JavaScript?

A string can contain attributes in the form like this:

let attr = ' min="0" step="5" max="100" '; 

or

let attr = ' min="2019-12-25T19:30" ';

etc.

Is there a function (either JS or jQuery) to assign these attributes to a HTML element?

Similar to setAttribute(name, value); but for multiple attributes.

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

0

There isn't any javascript method to do it, but you can convert your string to array and call setAttribute on the target element on every iterate of the array. like this:

let el = document.getElementById("targetId")  
let attrs = ' min="0" step="5" max="100" '.trim().replace(/\"/g,"").split(" ")
attrs.forEach(attr => {
  const [key,value] = attr.split('=');
  el.setAttribute(key,value)
})
about 4 years ago · Juan Pablo Isaza Report

0

Please look at the answer at the following Setting multiple attributes for an element at once with JavaScript

Quoting from there, you can use the following helper function

function setAttributes(el, attrs) {
  for(var key in attrs) {
    el.setAttribute(key, attrs[key]);
  }
}

And call it like so:

setAttributes(elem, {"src": "http://example.com/something.jpeg", "height": "100%", ...});
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!