I have introduced infinite scroll in my code and now the like script does not work: the page is refreshed when I push the like button.
Like-btn script:
<script>
$(document).ready(function(){
function updateText(btn, newCount, iconClass, verb){
verb = verb || "";
$(btn).html(newCount + ' <i class="' + iconClass + '"style="font-size:15px;"></i>' + verb )
btn.attr("data-likes", newCount)
}
$('.infinite-container').on("click", ".like-btn",(function(e){
e.preventDefault()
var this_ = $(this)
var likeUrl = this_.attr("data-href")
var likeCount = parseInt(this_.attr("data-likes")) | 0
var addLike = likeCount + 1
var removeLike = likeCount - 1
if (likeUrl){
$.ajax({
url: likeUrl,
method: "GET",
data: {},
success: function(data){
console.log(data)
var newLikes;
if (data.liked){
updateText(this_, addLike, "fas fa-heart")
} else {
updateText(this_, removeLike, "far fa-heart")
}
}, error: function(error){
console.log(error)
console.log("error")
}
})
}
})
})
</script>
Infinite scroll script
<script>
var infinite = new Waypoint.Infinite({
element: $('.infinite-container')[0],
onBeforePageLoad: function () {
$('.loading').show();
},
onAfterPageLoad: function ($items) {
$('.loading').hide();
}
});
</script>
Infinite scroll works correctly but in the elements charged after infinite scroll, the like-btn script does not work.
Could somebody see where is the problem?
Thank you for your help!