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

89
Visualizações
loop through html inputs that have an ID

I'm struggling here with some javascript code I need. I have an html document with more than 300 inputs (type text). All inputs have an id, some inputs also have a name (similar to the id). I want to loop through all input tags that have an ID but do not have a name and for each one add a name attribute identical to the ID.

I think it's simple and I know how to find or loop through elements with specific ID but there is something I'm missing and cant figure it out.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use querySelectorAll with the attribute selector [attr], loop through and check if it's got a name, if not copy the ID to the name.

eg.

for (const el of document.querySelectorAll('input[id]')) {
  if (!el.getAttribute('name')) {
    el.setAttribute('name', el.getAttribute('id'));
  }
}

console.log(document.querySelector('form').innerHTML);
<form>
  <input id="bob"/>
  <input id="john"/>
  <input name="gotname"/>
</form>

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