J.Zil
J.Zil

Reputation: 2449

Store input value as a var and display it

This is the code I am working from: http://jsfiddle.net/DYfbg/12/

If you click "map" then try typing a placename it gives you autocomplete suggestions. Then if you click on one of these suggestions it tells you the co-ordinates in an alert box.

What I want to do is capture what is in the input box when the autocomplete option is clicked, and then store it as a variable. Then in the alert box, along with the co-ordinates, I want to print the input box variable into it.

I know this all sounds pretty simple but I am new to javascript so I would really appreciate some input.

This is the segment of code:

google.maps.event.addListener(autocomplete, 'place_changed', function() {
    var place = autocomplete.getPlace();
    **// Store input box as variable**
    if (!place.geometry) {
        current_place = null;
        alert('Cannot find place');
        return;
    } else {
        current_place = place;
        alert('Place is at:' + place.geometry.location);
        **// Print input box varaible aswell**
    } });

EDIT: This is as close as I could get without getting stuck:

    // Set Autocomplete Location Variable equal to #loc input
    $("#loc").val(ac_location);
    // Print Autocomplete Variable
    alert(ac_location);

Upvotes: 0

Views: 229

Answers (1)

Draykos
Draykos

Reputation: 822

In the "place" object returned with

var place = autocomplete.getPlace();

you have all info you need Add a console.log and you'll see all infos returned. For example:

var place = autocomplete.getPlace();
console.log(place);
window.myGlobalVar = place.name;

Edit based on notes below: it seems original value of inputbox is actually stored in this property:

autocomplete.gm_accessors_.place.lf.d 

Upvotes: 1

Related Questions