I have a like button that users jQuery. I want to change the icon based on the like status(outlined heart to filled heart). The issue is when I click like it clears removes the icon
pre like:
post like:
How do I stop it from clearing the icon while still updating the like count, as well as changing the icon class
jQuery:
<script>
$(document).ready(function(){
function updateText(btn, newCount, verb){
btn.text(newCount + " " + verb)
}
$(".like-btn").click(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_, data.likescount, "like")
} else {
updateText(this_, data.likescount, "unlike")
}
}, error: function(error){
console.log(error)
console.log("error")
}
})
}
})
})
</script>
like button:
<a class="like-btn" data-href='{{ post.get_api_like_url }}' data-likes="{{ post.likes.count }}" href="{{ post.get_like_url }}"><i class="fa fa-heart-o like-heart" aria-hidden="true"></i>{{ post.likes.count }} </a>
if (data.liked){
updateText(this_, data.likescount, "")
$(".like-btn").prepend("<i></i>")
$( "i" ).addClass( "fa fa-heart-o like-heart" );
} else {
updateText(this_, data.likescount, "")
$(".like-btn").prepend("<i></i>")
$( "i" ).addClass( "fa fa-heart-o like-heart" );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.