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

202
Vistas
Js - Loop through array to create inputs and values only filling last item input value

I have a div with id="inputs" on html and the following code on js:

let paises=[
    {pais: "Honduras",scripttag:`<script src="perro loco come baba"> 'cha cha'` },
    {pais: "Chile",scripttag:"perropapa"},
    {pais: "Madagascar",scripttag:"otro"}
]
let inputDiv=document.getElementById("inputs")
for(let p of paises){
    if(p.scripttag){
        inputDiv.innerHTML+=`<input disabled id="` + p.pais + `">`
        let inputPais=document.getElementById(p.pais)
        inputPais.value=p.scripttag
    }
}

If the element of the paises array has a scripttag property, an input is created and value is filled with scripttag content.

Inputs get created properly, the issue is that in the page all the inputs are empty except the last one created (on this case the input with id Madagascar is filled with "otro")

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Your issue is that you are not creating a new element every time you want to add a new input.

Instead of adding html to divs like that, you can make use of createElement.

let paises=[
    {pais: "Honduras",scripttag:`<script src="perro loco come baba"> 'cha cha'` },
    {pais: "Chile",scripttag:"perropapa"},
    {pais: "Madagascar",scripttag:"otro"}
]
let inputDiv=document.getElementById("inputs")
for(let p of paises){
    if(p.scripttag){
        const newInput = document.createElement('input')
        newInput.id = p.pais
        newInput.value = p.scripttag
        inputDiv.appendChild(newInput)
    }
}

This way you are adding a new element to the div each loop. Instead of overwriting the previous element. I also think you have more control over the inputs properties using DOM manipulation instead writing everything out in a string and concating.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Another option is that you could just set the value with the HTML you are generating.

let paises=[
    {pais: "Honduras",scripttag:`<script src='perro loco come baba'/> 'cha cha'` },
    {pais: "Chile",scripttag:"perropapa"},
    {pais: "Madagascar",scripttag:"otro"}
]
let inputDiv=document.getElementById("inputs")
for(let p of paises){
    if(p.scripttag){
        inputDiv.innerHTML+=`<input disabled id="${p.pais}" value="${p.scripttag}">`
    }
}
<div id='inputs'></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

let paises=[
    {pais: "Honduras",scripttag:`<script src="perro loco come baba"> 'cha cha'` },
    {pais: "Chile",scripttag:"perropapa"},
    {pais: "Madagascar",scripttag:"otro"}
]
let inputDiv=document.getElementById("inputs")
for(let p of paises){
    if(p.scripttag){
        inputDiv.innerHTML= inputDiv.innerHTML +`<input disabled='true' id="` + p.pais + `" value='${p. scripttag}'/>`
    }
}
<div id="inputs"></div>

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