I have the following script, which only seems to get the value from the first iteration of the loop, and not the rest:
$(document).ready(function() {
get_jsonp_feed();
function get_jsonp_feed() {
$.ajax({
url: "http://query.yahooapis.com/v1/public/yql?q=%20SELECT%20*%20FROM%20html%20WHERE%20url%3D%22http%3A%2F%2Fnews.bbc.co.uk%2Fweather%2Fforecast%2F4276%3Fsearch%3Dgerrards%2520cross%26itemsPerPage%3D10%26region%3Dworld%26area%3DGerrards%2520Cross%22%20and%20xpath%20%3D%20'%2F%2Ftable%2Ftbody%2Ftr'&format=json&diagnostics=true&callback=cbfunc",
type: 'GET',
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'cbfunc',
error: function(xhr, status, error) {
alert(xhr.responseText);
},
success: function(data) {
var itemList = data.query.results.tr[0].td;
for (var i = 0; i < 5; i++) {
alert(itemList[i].div.abbr.content);
}
}
});
}
});
This displays Mon after which it displays Cannot read property 'content' of undefined. It should be displaying 5 alerts, Mon, Tue, Wed, Thu, and Fri.
jsfiddle: http://jsfiddle.net/DsEGT/.
You want to be looping through the
trelements, not thetdelements. Make thesuccesscallback: