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

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -