I am putting liking and unliking link for games.
Initially like is active where user click on like it will show
unlike link
1 like this game
that is ok.
But when we click on unlike link it reload the page.
I dont want to reload the page
<div class="gameLikeStatus">
<a href="likeit" id="likeitlink">Like</a>
</div>
<div class="totalLikes">
<span id="likesCount"><s:property value="likes"/></span> like this game<br>
</div>
<script>
$('a#likeitlink').bind('click',function(event){
event.preventDefault();
$('.gameLikeStatus').html('<a href="unlikeit" id="Unlikeitlink">Unlike</a>');
$likesNo= $('#likesCount').html();
$likesNo++;
$('#likesCount').html(""+$likesNo);
});
$('a#Unlikeitlink').bind('click',function(event){
event.preventDefault();
$('.gameLikeStatus').html('<a href="likeit" id="likeitlink">Like</a>');
$likesNo= $('#likesCount').html();
$likesNo--;
$('#likesCount').html(""+$likesNo);
});
</script>
Replace
with
(and the same for the other one)
When you’re executing
$('a#Unlikeitlink').bind, the collection is empty as the link doesn’t exist yet, so this does nothing. on enables the delegation to objects not yet existing. As you didn’t bind the callback, you weren’t preventing the normal link behavior, hence your problem.You should also parse the html that your increment or decrement :
(you could also more simply keep it in a global var instead of reading it each time)
Demonstration