Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using a little code for Like Unlike button! Problem is when i click on first Like link then all span.count value change.
JavaScript
<script>
$(document).ready(function(){
$('a.like').click(function () {
    if ($(this).attr('class') == 'like') {
        $.post("like.cfm", {postid:$(this).attr("postid"),uid:$(this).attr("uid")} );
 
        $(this).removeClass('like').addClass('unlike').html('Unlike');
		var test=$('.count').html();
        test++;
        $(".count").html(test);
    } else {
        $.post("unlike.cfm", { pageID: $(this).attr("postid") });
 
        $(this).removeClass('unlike').addClass('like').html('Like');
		
		var test=$('span.count').html();
        test--;
        $(".count").html(test);
    }
});
});
</script>


Here's the HTML

XML
<a href="##" class="like" postid="1234" uid="2">Like </a> <span class="count">100</span>
<br>
<br>
<a href="##" class="like" postid="1234" uid="2">Like </a> <span class="count">100</span>



JSFiddle http://jsfiddle.net/tgaQA/[^]
Posted
Comments
_Maxxx_ 6-Jun-14 10:49am    
Well you have

<pre lang="cs">var test=$('.count').html();
test++;</pre>

which says 'get the element with a class of .count and increment it.

And you have two elements with that class

sorry I don't know how to get it working off the top of my head - as you plainly do't want to give each element its own Id and function///
liquidsmoke07 6-Jun-14 11:06am    
Hey Maxx i got the solution :) You can see what Peter gave us :)
Brian A Stephens 6-Jun-14 13:37pm    
I would also caution against your use of $(this).attr('class'), since it will return ALL classes of the element. You may very well add another class for styling purposes that will remain in both the "like" and "unlike" states. It's more precise to use $(this).hasClass('like').

1 solution

Since the span is next to the like, just has to use next(), see modification:
$(document).ready(function () {
    $('a.like').click(function () {
        var next = $(this).next()
        var test = $(this).next().html();
        if ($(this).attr('class') == 'like') {
            $.post("like.cfm", {
                postid: $(this).attr("postid"),
                uid: $(this).attr("uid")
            });

            $(this).removeClass('like').addClass('unlike').html('Unlike');

            test++;

        } else {
            $.post("unlike.cfm", {
                pageID: $(this).attr("postid")
            });

            $(this).removeClass('unlike').addClass('like').html('Like');

            test--;
        }
        next.html(test);
    });
});
 
Share this answer
 
Comments
liquidsmoke07 6-Jun-14 11:05am    
Thanks Bro ! It's working good :)
Peter Leow 6-Jun-14 11:34am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900