James
James

Reputation: 1706

jQuery calculating two values wrong and not sure why

Any ideas why the total I'm trying to calculate together is coming out as 1.00?

$(document).ready(function() {
  // Hide initial values that need updating
  $("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").hide();
  // get current delivery rate
  $("#get_rate").trigger('click');
  // set a timeout and get total and shipping that was generated and add together for nnew total
  setTimeout(function() {
    // get cart sub-total
    var a = $(".cart-total span").html().replace(/[$£]/gi, "");
    var ib = $("#estimated-shipping em").html().replace(/[$£]/gi, "");

    if (ib == "FREE") {
      $("#estimated-shipping em").html("FREE");
      var b = "0.00";
    } else {
      var b = parseFloat($("#estimated-shipping em").text().replace(/\$|£/gi, ""));
    }

    // add together sub-total and estimated shipping to new total
    // update with new total with sub-total and added shipping
    var total = parseFloat(a) + parseFloat(b);
    total = parseFloat(Math.round(total * 100) / 100).toFixed(2);
    $('.cart-finalTotal span').text("£" + total);

    // show new values
    $("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").show();
  }, 2000);
  $(".item-quantity input").on("change", function() {
    document.location.href = location.href
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="cart-right">


  <p class="cart-total">Sub-Total<span class="money">£1,245.00</span>
  </p>
  <p class="cart-vat">VAT 20% (included)<span class="money" style="display: block;">£249.00</span>
  </p>
  <p class="cart-delivery">Delivery (estimated)<span class="money" id="estimated-shipping">+ <em style="display: inline;">FREE</em></span>
  </p>
  <p class="cart-finalTotal">Total<span class="money" style="display: block;">£0.00</span>
  </p>

</div>

Upvotes: 0

Views: 114

Answers (1)

Beamer180
Beamer180

Reputation: 1525

In your subtotal, you have a comma, parseFloat will stop when it see a non numeric character that isn't a dot. So parse float is returning a 1 not a 1245. You need your regex to be something more on the lines of /[^0-9.]/g.

Here is code that is working:

$(document).ready(function() {
  // Hide initial values that need updating
  $("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").hide();
  // get current delivery rate
  $("#get_rate").trigger('click');
  // set a timeout and get total and shipping that was generated and add together for nnew total
  setTimeout(function() {
    // get cart sub-total
    var a = $(".cart-total span").html().replace(/[^0-9.]/gi, "");

    if ($("#estimated-shipping em").text() == "FREE") {
      $("#estimated-shipping em").html("FREE");
      var b = "0.00";
    } else {
      var b = parseFloat($("#estimated-shipping em").text().replace(/[^0-9.]/gi, ""));
    }

    // add together sub-total and estimated shipping to new total
    // update with new total with sub-total and added shipping
    var total = parseFloat(a) + parseFloat(b);
    total = parseFloat(Math.round(total * 100) / 100).toFixed(2);
    $('.cart-finalTotal span').text("£" + total);

    // show new values
    $("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").show();
  }, 2000);
  $(".item-quantity input").on("change", function() {
    document.location.href = location.href
  });
});

Upvotes: 1

Related Questions