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

217
Visualizações
How to create HTML dynamic select option from JSON hash?

I have a simple $test value code:

$test = A,bb,100|A,ff,200|A,ee,100|D,ee,3|D,gg,10|R,ii,7

I have a select tag in my HTML:

<select name="datas" id="datas"></select>

I need a simple way to create HTML select box from this JSON, like this:

<select>
 <option value="">A</option>
 <option value="100">bb</option>
 <option value="200">ff</option>
 <option value="100">ee</option>
</select>
<select>
 <option value="">D</option>
 <option value="3">ee</option>
 <option value="10">gg</option>
</select>
<select>
 <option value="">R</option>
 <option value="7">ii</option>
</select>
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

You mention JSON, a pre-existing select and the "php" tag although I don't see how any of those things are relevant to your question.

Please see the code below to produce a select with its content, per your requirements:

// This isn't JSON. It's just a string
let $test = "A,bb,100|A,ff,200|A,ee,100|D,ee,3|D,gg,10|R,ii,7";

// Split the initial string where there are | characters and then loop 
// over the resulting array of strings
$test.split("|").forEach(function(values){
  // Create a new `select` element
  const sel = document.createElement("select");
  
  // Split the part of the string that contains the next level
  // values and loop over them
  values.split(",").forEach(function(value){
    // Create an `option` element 
    const opt = document.createElement("option");
    opt.textContent = value;  // Set the value to the individual string
    sel.appendChild(opt); // Append the option to the select
  });
  document.body.appendChild(sel);  // Add the select to the page.
});

about 4 years ago · Santiago Gelvez 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