1

i have jquery script with operator minus, but i got wrong values, this is my code

var fuid = $(this).attr("data-delete-id"); 
var hargaProduk = parseInt($("#harga_"+fuid).text());
var hargaTotal = parseInt($("#total-harga").text());
var newValue = hargaTotal - hargaProduk;            
$("#total-harga").html(newValue);

And this is my HTML Code

   // foreach in here
   <tr id="table_1">
        <td>Data 1</td>
        <td>Rp <span id="harga_1"> 275,000</span></td>
        <td><span class="delete-product" data-delete-id="1"><span>Delete</span></span></td>
      </tr>
   <tr id="table_2">
        <td>Data 2</td>
         <td>Rp <span id="harga_2"> 175,000</span></td>
        <td><span class="delete-product" data-delete-id="2"><span>Delete</span></span></td>
      </tr>
   <tr id="table_3">
        <td>Data 3</td>
         <td>Rp <span id="harga_3"> 180,000</span></td>
        <td><span class="delete-product" data-delete-id="3"><span>Delete</span></span></td>
      </tr>
  //end foreach in here
  <tr style="background:#DEDEDE;">
            <td><b>Total </b></td>
            <td></td>
            <td><b>Rp <span id="total-harga"><?= number_format(CartTotal())?></span></b></td>
            <td></td>
        </tr>

When i run it, it displaying like this: enter image description here

But when i click delete in number 3 and run JS script value change to Rp -179

Is there something wrong with my code? i just want to display value from variable hargaTotal - hargaProduk

Thankyou

Muhamad Yulianto
  • 1,573
  • 3
  • 22
  • 32

2 Answers2

4

JavaScript's parseInt and parseFloat stop at the first invalid character, and they don't support commas, so parseInt("180,000") returns 180. To parse your numbers, you'll need to remove the commas:

var hargaProduk = parseInt($("#harga_"+fuid).text().replace(/,/g, ""));
// ------------------------------------------------^^^^^^^^^^^^^^^^^^

...and then add them back when displaying; if you search, you'll find solutions for formatting numbers with thousands separators here on SO, such as this one.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Its working mate, i just need to replace the value in variable newValue back as a price format – Muhamad Yulianto Jun 15 '15 at 13:42
  • yes, i got new value in just number format ex: 1175000, and i want to change it back to price format 1,175,000 Thanks – Muhamad Yulianto Jun 15 '15 at 13:45
  • @AlanEl-ninoMalmsteen: That's what the link at the end of the answer is for; it's to an SO question about doing that, which has several answers. (That's just one of several, so if for any reason you're not happy with that one, a search on SO will give you several options.) – T.J. Crowder Jun 15 '15 at 13:45
0

it getting problem for comma(,).use this JavaScript code

    var fuid = $(this).attr("data-delete-id");
    var hargaProduk = parseInt( $("#harga_"+fuid).text().replace(/,/g, ""));
    var hargaTotal = parseInt( $("#total-harga").text().replace(/,/g, ""));
    var newValue = hargaTotal - hargaProduk;
    $("#total-harga").html(newValue);
Imtiaz Pabel
  • 5,307
  • 1
  • 19
  • 24