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

251
Visualizações
How to update json value with dropdown list

Can anyone teach me how can I update json value using the the dropdown list. In JavaScript

HTML:

<select id="currency" onchange="getSelectValue();">
    <option value="Singapore Dollar">Singapore Dollar</option>
    <option value="USD">USD</option>
    <option value="Malaysia Riggit">Malaysia Riggit</option>
</select>

JSON :

{
"products": [
    {
       "name": "Apple",
        "price": "2.50",
        "code": "SGD"
    },
    {
       "name": "Orange",
        "price": "2.50",
        "code": "SGD"
    }
]
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can create a select in javascript like this, focus on the <script />

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Javascript - Select</title>
</head>
<body>
    <div id="main"></div>
</body>
<script>
    const {products} = JSON.parse('{ "products": [ { "name": "Apple", "price": "2.50", "code": "SGD" }, { "name": "Orange", "price": "2.50", "code": "SGD" } ] }');

    const select = document.createElement("select");
    select.id = 'currency';
    select.onchange = ({target: {value}}) => console.log(value);

    products.forEach(({code, name}) => {
        const option = document.createElement("option");
        option.label = name;
        option.value = code;
        select.appendChild(option);
    });

    const main = document.getElementById('main');
    main.appendChild(select);
</script>
</html>

EDIT:

Add the "value" as a param of the function

<select id="currency" onchange="getSelectValue(value)">
    <option value="SGD">Singapore Dollar</option>
    <option value="USD">USD</option>
    <option value="MR">Malaysia Riggit</option>
</select>

and the function definition should look like this:

const getSelectValue = (selectedValue) => {
    console.log(selectedValue);
    // Your code...
};
about 4 years ago · Juan Pablo Isaza Relatório

0

Here is the code sample.

var json=JSON.parse( "Your json which you mentioned above");

var products= json.products;



string selectlist="";

if(products.length>0)
{
    
    selectlist += "<select>";
    for(var i = 0; i < products.length; i++) {


        var currency = json[i];

            selectlist += "<option value="+currency.code+"> "+currency.code+"  </option>";
    }   
    selectlist += "</select>"
if("body").append(selectlist);
}
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