Reputation: 134
I have a very basic question, how do I use the subtraction with percentage? More important is there even one for that? What's the page to the jQuery subtraction? I can't seem to find one.
What I want to do is a simple task but with no knowledge about subtracting percentage I can't do it.
I use data-val
in the div
to store the value, like in the HTML
i have data-val="10"
10 as in 10%, so how do I make the jQuery to take the 10 in the data-val
and subtract it?
<td width="200">Discount:</td>
<td width="100" align="right" class="discount" data-val="10">-29,8 kr</td>
with the following jQuery code.
jQuery(document).ready(function($){
$('#cart_review .quantity').change(function (event) {
$quan = $(this);
console.log($quan.parent().next()[0]);
$quan.parent().next().find('.price').text(function () {
return $quan.val() * parseInt($(this).attr('data-val'), 10) + ' kr';
});
var total = 0;
$('#cart_review .price').each(function(k, v){
total += parseFloat($(v).text(), 10);
});
$('.products').text(function() {
return total + ' kr';
});
var shipping = 0;
$('.shipping').text(function () {
shipping = parseInt($(this).attr('data-val'), 10);
return $(this).attr('data-val' + ' kr');
});
$('#cart_review #total').text(total + shipping + ' kr')
});
});
Upvotes: 1
Views: 4091
Reputation: 780808
jQuery doesn't have any arithmetic functions, you just have to use the built-in Javascript operators.
var discount_pct = parseInt($(".discount").data("val"), 10);
var discount = -(total * discount_pct/100); // percent means divide by 100
You can put the amount of discount in the discount field with:
$('.discount').text(-discount + ' kr');
Upvotes: 5