var $arrow = $(this);
var $sibling = $arrow.siblings('span.arrow');
var $score = $arrow.siblings('span.score');
var vote = $arrow.hasClass('up') ? 'up' : 'down';
var alreadyVoted = $sibling.hasClass('voted');
if (!USER_LOGGED_IN)
{
alert('You must be logged into vote');
}
else if (!$arrow.hasClass('voted'))
{
if (alreadyVoted)
$sibling.removeClass('voted');
$arrow.addClass('voted');
$score[0].innerHTML = parseInt($score[0].innerHTML) + ((vote == 'up') ? 1 : -1);
}
I have an upvote and downvote button. A 'current score' is shown next to these buttons which I would like to increment/decrement when a vote is cast.
For example, if they load the page and see the score is 200
. When they upvote, the score will change to 201
. When they downvote, the score needs to change to 199
. Why? Because if they downvote after upvotting (change their mind) then the vote needs to go from the original score. Not the new score they created by upvotting.
Basically, if they upvote and then downvote, the score, currently, goes back to the original score. Their vote isn't cast.
I'm having trouble making this work so their vote is cast...