I've got this working JavaScript (example is [here][1]), which should work the same as on the Stack Exchange network.
How can I simplify it a bit?
function yellow() {
return 'rgb(255, 255, 0)';
}
$(function() {
$(".post-upvote").click(function() {
// ajax(url + "upvote/" + $(this).attr('data-postid'), false, false);
if ($(this).parent().children('.post-downvote').css('background-color') == yellow()) { // user upvoted so let's delete upvote
$(this).parent().children('.post-votes').text(parseInt($(this).parent().children('.post-votes').text()) + parseInt(1));
}
$(this).parent().children('.post-downvote').css('background-color', 'white');
if ($(this).css('background-color') == yellow()) { // if it's yellow, user is canceling his downvote
$(this).css('background-color', 'white');
$(this).parent().children('.post-votes').text(parseInt($(this).parent().children('.post-votes').text()) - parseInt(1));
}
else {
$(this).css('background-color', 'yellow');
$(this).parent().children('.post-votes').text(parseInt($(this).parent().children('.post-votes').text()) + parseInt(1));
}
});
$(".post-downvote").click(function() {
// ajax(url + "downvote/" + $(this).attr('data-postid'), false, false);
if ($(this).parent().children('.post-upvote').css('background-color') == yellow()) { // user upvoted so let's delete upvote
$(this).parent().children('.post-votes').text(parseInt($(this).parent().children('.post-votes').text()) - parseInt(1));
}
$(this).parent().children('.post-upvote').css('background-color', 'white');
if ($(this).css('background-color') == yellow()) { // if it's yellow, user is canceling his downvote
$(this).css('background-color', 'white');
$(this).parent().children('.post-votes').text(parseInt($(this).parent().children('.post-votes').text()) + parseInt(1));
}
else {
$(this).css('background-color', 'yellow');
$(this).parent().children('.post-votes').text(parseInt($(this).parent().children('.post-votes').text()) - parseInt(1));
}
});
});