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

146
Vistas
Create the Dynamic dropdown in Javascript

Anyone guides me, How to create the dynamic dropdown using the below array/JSON in javascript?

 [
    {
      "1": "001",
      "2": "002",
      "3": "003"
    }
];

What I have done :

 let text = "<select class='form-control'><option value=''>All</option>";
                        
                        var array = [
                            {
                                "1": "001",
                                "2": "002",
                                "3": "003"
                            }
                        ];
                       
                        for (var i = 0; i < array.length; i++) {
                            // POPULATE SELECT ELEMENT WITH JSON.
                            text += '<option value=' + array[i]["1"] + '>'+ array[i]["1"] +'</option>';
                        }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Creating Select element using JavaScript:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to create a SELECT and an OPTION element.</p>

<button onclick="myFunction()">Try it</button>

<script>
const customItems = [
    {
      "1": "001",
      "2": "002",
      "3": "003"
    }
];

function myFunction() {
  const x = document.createElement("SELECT");
  x.setAttribute("id", "mySelect");
  document.body.appendChild(x);
  
  for (let i = 0; i < customItems.length; i++) {
    for (let key in customItems[i]) {
        const z = document.createElement("option");
        z.setAttribute("value", customItems[i][key]);
        const t = document.createTextNode(customItems[i][key]);
        z.appendChild(t);
        document.getElementById("mySelect").appendChild(z);
    }
    
  }
}
</script>

</body>
</html>

about 4 years ago · Juan Pablo Isaza Denunciar

0

use Object.entries() to read field in object.

var array = [
  {
    "1": "001",
    "2": "002",
    "3": "003"
  }
];
var x = document.createElement("SELECT");
x.setAttribute("id", "mySelect");
document.body.appendChild(x);

for (const [key, value] of Object.entries(array[0])) {
  var z = document.createElement("option");
  z.setAttribute("value", key);
  var t = document.createTextNode(value);
  z.appendChild(t);
  document.getElementById("mySelect").appendChild(z);
}       

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