Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!