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.
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)
})
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%", ...});