Im trying to perform simple calculations using jQuery but I'm getting NaN as result.
Here is my javascript:
$(document).on('click','.button-group button:not(.done)', function(e){
e.preventDefault();
$(this).addClass('active');
var votevalue = $(this).data('votevalue');
var image_id = $('.mainimage').data('image_id');
var votes = $('.mainimage').data('numvotes');
var totalscore = $('.mainimage').data('totalscore');
$.ajax({
type: 'POST',
url: '?a=vote',
data: {
"votevalue" : votevalue,
"image_id" : image_id
},
success: function(){
var votes = parseInt(votes);
votes++;
var average = ((parseInt(totalscore) + parseInt(votevalue)) / votes);
$('#vote-incremenet').html(votes);
$('#display-average').html(average);
$('#display-average').show();
$('.button-group button').addClass('done');
}
}); // end ajax
}); // end click
What am I doing wrong?
NaNissue, shouldn't you also be storing the newvotesvalue back into$('.mainimage').data('numvotes')? Otherwise when another button is clicked it will use the oldnumvotesvalue for its calculation.