This one’s just weird to me. Some little syntax error or something. Simply adding the value of two variables and appending them to a third.
var total = 0;
input.each(function(){
var thiis = $(this),
cost = thiis.attr('data-cost');
if (thiis.prop('checked')){
total = total + cost;
} else {
total = total - cost;
}
}
So if cost = 5 and the input is checked then total equal 5. Instead, I’m getting 05.
Update:
Word is that the values arn’t integers, so they’re appending. Instead of having to do some fancy string-integer flip, is there a way for me to grab the data attribute as an integer from the get-go?
Thanks!
you need an integer cast if somewhere these vars are casted as strings, which is the reason for the incorect result.
and you could do just
total += parseInt(each);