JSFiddle - React, Tailwind, and code Playground
HTML
<div class="vote">
<a class="arrow likes vote-up" dir="up" id="112">^</a>
<div style="color: #296394;" class="count">
1 </div>
<a class="arrow down vote-down" dir="down" id="112">V</a>
</div>
JavaScript
$(function() {
var down = $('.vote-down');
var up = $('.vote-up');
var direction = up.add(down);
var largeCount = $('#js-large-count');
var totalUp = $('#js-total-up');
var totalDown = $('#js-total-down');
var totalUpCount = parseInt(totalUp.text(), 10);
var totalDownCount = parseInt(totalDown.text(), 10);
var castVote = function(submissionId, voteType) {
/*
var postURL = '/vote';
$.post(postURL, {
submissionId : submissionId,
voteType : voteType
} , function (data){
if (data.response === 'success') {
totalDown.text(data.downvotes);
totalUp.text(data.upvotes);
}
}, 'json');
*/
alert('voted!');
};
$(direction).on('click', direction, function () {
// The submission ID
var $that = $(this),
submissionId = $that.attr('id'),
voteType = $that.attr('dir'), // what direction was voted? [up or down]
isDown = $that.hasClass('down'),
isUp = $that.hasClass('up'),
curVotes = parseInt($that.parent().find('div.count').text(), 10); // current vote
castVote(submissionId, voteType);
// Voted up on submission
if (voteType === 'up') {
var alreadyVotedUp = $that.hasClass('likes'),
upCount = $that.next('div.count'),
dislikes = $that.nextAll('a').first(); // next anchor attr
if (alreadyVotedUp) {
// Clicked the up arrow and previously voted up
$that.toggleClass('likes up');
if (totalUpCount > totalDownCount) {
upCount.text(curVotes - 1);
largeCount.text(curVotes - 1);
} else...