Arti Patel Arti Patel - 6 months ago 61
Javascript Question

How to create LIKE and UNLIKE button in jquery

I want to create LIKE and UNLIKE button in jquery, what i did is ,

<span><a class="like-Unlike" href="">Like</a></span> |
function LikeUnLikeButton() {
$(".like-Unlike").click(function (e) {
if ($(this).html() == "Like") {
$(this).html('Unlike');
}
else {
$(this).html('Like');
}
});
}


but it is not working , problem is when i click on LIKE button i can see unlike button and again it's back to LIKE button

Please check it out here : http://jsfiddle.net/mHJnH/

Answer

You have miss placed closing parenthesis of $(this ) is missing here.

Change

  $(this.html('Unlike'));

To

 $(this).html('Unlike');

Edit due to update in question

You may not need function LikeUnLikeButton() {

Live Demo

$(".like-Unlike").click(function(e) {
    if ($(this).html() == "Like") {
        $(this).html('Unlike');
    }
    else {
        $(this).html('Like');
    }
    return false;
});‚Äč
Comments