I remember having this problem once before but I dont’ know which project it was on or how I fixed it.
I have an ajax ‘add to favorite’ button, and I also have infinite scroll which ajax loads pages when you reach the bottom of the page. The problem is the ajax doesn’t apply to the newly appended content:
// Favorites
$('.res-list .star').click(function(){
var starLink = $(this);
var listItem = $(this).parent().parent();
if ($(starLink).hasClass('starred')) {
$(starLink).removeClass('starred');
} else {
$(starLink).addClass('starred');
}
$.ajax({
url: '/favorites/?id=' + $(starLink).attr('data-id'),
type: 'PUT',
success: function() {
if ($(starLink).hasClass('starred')) {
$(listItem).animate({
backgroundColor: '#FFF8E4'
}, 400);
} else {
$(listItem).animate({
backgroundColor: '#ffffff'
}, 400);
}
}
});
return false;
});
You need live event
or use delegate()
read about delegate()
read about on()