javascript - Display JSON Objects as options in dropdown -
i have simple html page in read objects json file , show them options in dropdown using javascript. html , json below :
html
<html> <body> <select id="myid">mylist</select> <script src="myscript.js"></script> </body> </html>
json
[{"mode":"card"}, {"mode":"cash"}, {"mode":"cheque"}];
any w.r.t. javascript appreciated!
working , tested code, reading external local json file (data.json) using javascript, first create data.json file below data:
data = '[{"mode":"card"}, {"mode":"cash"}, {"mode":"cheque"}]';
mention path of json file in script tag
<html> <script type="text/javascript" src="data.json"></script> <script> function addoptions(){ var jsonarray = json.parse(data); var select = document.getelementbyid('dd'); var option; (var = 0; < jsonarray.length; i++) { option = document.createelement('option'); option.text = jsonarray[i]["mode"]; select.add(option); } } </script> <body onload="addoptions();"> <select id="dd"></select> </body> </html>
Comments
Post a Comment