Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda