I currently have this
$(document).ready(function(){
like = "like";
current_likes = parseInt(50);
$("#add-love").click(function(){
if(like = "like"){
$("#my_like").removeClass('hidden-profile');
current_likes++;
$("#like_count").html(current_likes);
like = "unlike";
console.log(like);
}else{
$("#my_like").addClass('hidden-profile');
current_likes--;
$("#like_count").html(current_likes);
like = "like";
}
});
});
But when i click the like button, it updates the like counter and also the variable (i have checked this by logging it) But when it is clicked the second time it doesnt fall into the second part of the if statement, rather it just acts as if like=”like” still?
You’re assigning
=, rather than comparing==.Note that you have declared the variable
liketowindowscope, since you didn’t use thevarkeyword. If it should be scoped only to theready()function, usevar like = 'like';inside theready()function. If you do intend it atwindowscope, it is advisable to use thevarkeyword outside theready()function.