Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to use the google maps places json to populate a dropdown form element.

I have tried reading many pages but cannot figure it out.

I am sure this is something simple for someone who knows the scripting. I do not.

Please help if you can.
Thanks,
Removed@gmail.com
[Edited - Please do not provide personal email-address in public forum.
You will get a notification email whenever someone post an answer.]
Posted
Updated 17-Apr-12 13:53pm
v3

1 solution

Try something like this:

This is your JSON object. Realistically, it would be loaded from your server.

JavaScript
<script type="text/javascript">
    var jsonObj = { location: [
        { "name": "france", "address": "merde" },
        { "name": "london", "address": "shiite" }
    ]
    }
</script>


In your page, assume you have a <select> element with an id of "dropDown"

JavaScript
<select id="dropDown"></select>


Finally, when your page has finished loading, this code will populate your drop-down list.

JavaScript
var myDDL = document.getElementById("dropDown");

for (i = 0; i < jsonObj.location.length; i++) {
    var option = document.createElement("option");
    option.text = jsonObj.location[i].name;

    option.value = jsonObj.location[i].address;
    try {
        myDDL.options.add(option);
    }
    catch (e) {
        alert(e);
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900