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

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

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 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