I display user comments. Each comment is one div and each div has <a> tag with class ‘commentLikeLink‘. I bind jquery click event to ‘commentLikeLink‘ class but If I have 10 comments and click on one like button I get event fired 10 times.
I know that this happen’s because I have same class multiple times. But How to stop this?
Here’s the code:
...
<div class="commentBox"">
...
@Html.ActionLink(likeText, "LikeComment", "Comment", null, new { id = Model.CommentId, @class = "commentLikeLink" })
...
Event code:
$(function () {
$('.commentLikeLink').click(function (event) {
var commentId = event.target.id;
$.ajax({
url: this.href,
type: 'POST',
data: { commentId: commentId },
context: this,
success: function (result) {
if (result.msg == '1') {
$(this).text('Dislike');
}
else if(result.msg == '2') {
$(this).text('Like');
}
}
});
return false;
});
});
I had this happen before when i accidently included the same .click script function multiple times in the page. Make sure your javascript is only included once