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.

JSfiddle

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);
});
Was it helpful?

Solution

JSfiddle

$(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);
    });

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top