1👍
You need to add the item primary key in the like_function
call:
<button onclick="like_function({{ posts.pk }})" id = "like" class="btn btn-link"><i class="fa fa-heart"></i> </button>
then you define the like_function
as:
function like_function(id) {
fetch(`index/${id}/like`)
.then(response => response.json())
.then(result => {
if(result.is_like){
document.querySelector('#like').innerHTML = "Like";
}
else{
document.querySelector('#like').innerHTML = "Unlike";
}
})
}
Source:stackexchange.com