One of many possible ways:
var previous = null; $(data).find('item').each(function(i, value){ if (previous) { ... do stuff with previous element ... } var title = $(this).find('title').text().replace(/\w+\s+\(.*?\)/, ""); var link = $(this).find('link').text(); var pubDate = $(this).find('pubDate').text(); alert(title); alert(link); alert(pubDate); $('#events').append("<p>" + title + "<br/>" + link + "</p>"); previous = this; });
You need to check if the previous value is null, since the first iteration will contain a null value (the first element does not have the previous element).
source share