-1

today, i learned the basics of Json and its parsing through Jquery. I know my question is futile but i am stuck and unable to get a way out.

the following code doesn't work....but i guess i did everything ok

<html>
<head>
<div id="placeholder"></div>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>

<script>
$(document).ready(function(){

$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=2d34860785184c2e3458de2bc437ecfc&tags=rose&format=json&nojsoncallback=1&api_sig=b7303e1d0d8a9fbb1159404ca7927e98",function(data){
var output="<ul>";
for (var i in data.photos) {
    output+="<li>" + data.photos.photo[i].id+"</li>";


    if (i === 3){
    return false;
}
}
output+="</ul>";

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

<body>


</body>

</html>
7
  • can you post an error from the javascript console? Commented Aug 3, 2013 at 13:01
  • 1
    Why is your placeholder div inside the <head> of the document instead of the <body>? Commented Aug 3, 2013 at 13:02
  • I guess you didn't do everything ok if it doesn't work. Commented Aug 3, 2013 at 13:03
  • If you used indentation it'd be incredibly much easier to figure out everything. Commented Aug 3, 2013 at 13:04
  • if you use jquery,use the $("#id") selector..not getElementById Commented Aug 3, 2013 at 13:04

1 Answer 1

2

You have your indexes wrong in the JSON you need to make the following changes.

Demo: http://jsfiddle.net/ALJkV/

$(document).ready(function () {

    $.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=2d34860785184c2e3458de2bc437ecfc&tags=rose&format=json&nojsoncallback=1&api_sig=b7303e1d0d8a9fbb1159404ca7927e98", function (data) {
        var output = "<ul>";
        for (var i in data.photos.photo) { // The photo index is what you need;

            // If you want to skip 3
            if (i == 3) continue;
            // Or if you want to stop at 3 
            if(i == 3) break;

            output += "<li>" + data.photos.photo[i].id + "</li>";
        }
        output += "</ul>";

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

Continue: https://developer.mozilla.org/en...

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

17 Comments

why the hell continue?
continue does absolutely nothing here. I assume the OP wants to limit the loop to max 4 photos.
the way you did it, you will not skip anything. Either remove the continue or replace it with break.
@MightyPork - regarding your suggestion about using i.id, no, that would not work. i is the index/key not the associated value, so that would give an error.
On the return false; idea, that won't just exit the loop, it'll exit the entire function before it has actually put them in the placeholder element. Use break; to exit the loop without exiting the function.
|

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.