Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda