2

i am new to Json and Jquery.

Today i made this little code but its not working. I guess i wrote it right, but i know there is some little mistake somewhere. can you please help me to figure it out?

    <!doctype html>
    <html lang="en">
    <head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    </head>

    <script>

    $(document).ready(function () {

    $(":button").click(function(){

     var add = $("#destination").val();

    $.getJSON("http://maps.googleapis.com/maps/api/geocode/json?address= + add   +&sensor=false", function (data) {
        var output = "<ul>";


            output += "<li>"+ results.geometry.location.lat + " " + results.geometry.location.lng  + "</li>";
            output += "</ul>";

        document.getElementById("placeholder").innerHTML = output;
    });
    });
});
    </script>
    <body>

    <input type="text"  id="destination" /><button type="button">Search</button><br>
    <div id="placeholder"> </div>
</body>

</html>
1
  • Your getJSON function takes a callback with data as an argument and uses result in the function Commented Aug 4, 2013 at 7:32

3 Answers 3

2

Fixed. Note that results is an array.

<script>

$(document).ready(function () {

$(":button").click(function(){

 var add = $("#destination").val();

$.getJSON("http://maps.googleapis.com/maps/api/geocode/json?address= + add   +&sensor=false", function (data) {
    var output = "<ul>";


        output += "<li>"+ data.results[0].geometry.location.lat + " " + data.results[0].geometry.location.lng  + "</li>";
        output += "</ul>";

    document.getElementById("placeholder").innerHTML = output;
});
});

});

<input type="text"  id="destination" /><button type="button">Search</button><br>
<div id="placeholder"> </div>

Sign up to request clarification or add additional context in comments.

1 Comment

yea , it works....i am afraid that i always do mistake while considering arrays in Json..it confuses me...yesterday, i made a similar mistake.....what i understand is i must analyse the correct array ..otherwise, i would access an undefined element. thank you
1

I suppose that there's a mistake in URL. It should probably be:

"http://maps.googleapis.com/maps/api/geocode/json?address="+add+"&sensor=false"

because add is a variable defined earlier.

Other then that you should also do (since you are using jQuery):

$("#placeholder").html(output);

instead of

document.getElementById("placeholder").innerHTML = output;

And finally (as noted by @pixelcdv) you probably want

$.getJSON(..., function (results) {

because you are using results later, not data.

Comments

1

Your jquery selector doesn't need a colon, since you're referring to an element name

$("button")

As mentioned by freakish, when creating your url string, you need to close quotes before using "+" to concatenate strings.

You figured out that you're getting an array. Cool.

I made a fiddle if you want to have a look. Creating your json request and inspecting it carefully in a browser (Chrome renders json nicely) helps a lot.

my solution on jsfiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.