Try using the filter
method to remove any duplicates. I assume you have a #container
div that contains all teh div.element
items. In your example we are filtering all the $newElements
that are not found in the #container
div.
$container.infinitescroll({
navSelector: "div.paginate",
nextSelector: "div.paginate a",
itemSelector: "div.element",
loading: {
finished: undefined,
finishedMsg: "<em>Congratulations, you've reached the end of the internet.</em>",
img: "public/img/ajax-loader.gif",
msg: $('<div id="infscr-loading"><img alt="Loading..." src="public/img/ajax-loader.gif" /><div></div>'),
},
errorCallback: function() {
alert('no discounts');
},
}, function(newElements) {
var $newElements = $(newElements).css({opacity: 0});
//remove the first item
$newElements.splice(0, 1);
//remove any repeated discounts
return $newElements.filter(function(i, el) {
return !$('#container').find('#' + $(el).attr('id')).length
})
});
I forked your fiddle and added a filter function as well.
var newElements = ['<div class="element" id="id1">1</div>', '<div class="element" id="id4">4</div>', '<div class="element" id="id5">5</div>'];
$('.update').click(function(e) {
var uniqueElements = $(newElements).filter(function(i, el) {
return !$('#container').find('#' + $(el).attr('id')).length
});
console.log( uniqueElements );
});
http://jsfiddle.net/aBSP2/