$(function() {
$('a#btn').click(function() {
var discount = parseFloat($("#discount").data("discount"));
var increment = parseFloat($("#increment").text());
var newDiscountVal = (discount + increment).toFixed(2);
$("span#discount").text(newDiscountVal);
$("#discount").data("discount", newDiscountVal);
});
});
Incrementing a decimal based on data attribute string value
-
12-07-2023 - |
Question
I have some jquery taking a string value from a data attribute, and then increasing the value based on the value of value
. I am expecting it to increment the value each time and write the new total to the data attribute, however it does not work as expected.
The JQ
$("#btn").click( function() {
discount = $("#thisid").data("discount_tendered");
// alert(discount);
value = $("#value").text();
valueDec = parseFloat(value);
// alert(value);
newDiscountVal = discount += valueDec;
// alert(newDiscountVal);
$("#thisid").attr("data-discount_tendered", newDiscountVal);
});
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow