Click here to Skip to main content
14,361,384 members
Rate this:
Please Sign up or sign in to vote.
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 14:53pm
v3

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Try something like this:

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

<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"

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


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

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);
    }
}
   
v2

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100