0
votes

i am trying to get twitter feed using jQuery json i can get the below code to work, if i don't use a for loop. can anyone guide me how can i get it work to get multiple tweets? while i try to use another url : https://twitter.com/statuses/user_timeline/maxlibin.json?callback=twitterCallback2&count=3 still don't work

jQuery.getJSON("https://twitter.com/statuses/user_timeline/maxlibin.json?callback=?", function(data) {
    for (i = 0; i < 4; i++) {
        jQuery("#main .left").html(data[0].text);
    });​
2

2 Answers

2
votes
jQuery.getJSON("https://twitter.com/statuses/user_timeline/maxlibin.json?callback=?", function(data) {
    for (i = 0; i < 4; i++) {
        jQuery("#main .left").append(data[i].text);
    };
});​

http://jsfiddle.net/QfSbs/

With a little more work, you can stick each one in a separate paragraph and automatically link the URLs:

jQuery.getJSON("https://twitter.com/statuses/user_timeline/maxlibin.json?callback=?", function(data) {
    for (i = 0; i < 4; i++) {
        jQuery("#main .left").append($('<p>').html(data[i].text.replace(/(http:\/\/[^\s]+)\b/g, '<a href="$1">$1</a>')));
    };
});
​

http://jsfiddle.net/QfSbs/1/

0
votes

Each time you use html(...) You override anything inside the element.

Extract all the data, then append it to the element:

var output = "";
for (i=0; i<4; i++){
    output += data[i].text;
}
jQuery("#main .left").html(output);

Note that you didn't use the loop index, you just used 0 for all the iterations: ...html(data[0].text);