0

If I enabled/disabled the checkbox button, the added .00 removed from the amount.

How can I add the .00 to the amount?

Below is my code:

function checks() {
    document.getElementById('other_donation_check').checked = false;
}

function add(total, this_chk_bx) {
    var thetotal = form2.thetotal.value;

    if (this_chk_bx.checked == true) {
        //add if its checked
        var count=0;
        form2.thetotal.value = Number(thetotal) + Number(total);
        count++;
    } else {
        //subtract if its unchecked
        form2.thetotal.value = thetotal - total;
    }
}

and I created a fiddle.

How can I solve this issue?

nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
Twinxz
  • 303
  • 5
  • 16
  • 5
    Number.toFixed(2) to fixed turns it into a string with n decimals – Joel Harkes May 18 '17 at 05:46
  • [**`.toFixed()`**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) – Mi-Creativity May 18 '17 at 05:51
  • i tried .toFixed(2) method but i didnt get. can you please check my code "var thetotal = form2.Number(thetotal).value.toFixed(2);" – Twinxz May 18 '17 at 05:54
  • 1
    Possible duplicate of [JS - Format number with 2 decimal not rounded](http://stackoverflow.com/questions/29941224/js-format-number-with-2-decimal-not-rounded) – prasanth May 18 '17 at 05:56
  • sorry i tried like this but didnt work. – Twinxz May 18 '17 at 06:13
  • `.toFixed()` should definitely work @Twinxz. Take a close look at your attempt and the possible error you got. – nyedidikeke May 18 '17 at 06:20
  • @ nyedidikeke. var thetotal = form2.thetotal.value; how can i add .toFixed() function above code. – Twinxz May 18 '17 at 06:25
  • ^ `parseFloat(form2.thetotal.value, 10).toFixed(2)` ? You have nested object there, make sure each value is correct first. – Chay22 May 18 '17 at 06:47
  • @Chay22, var thetotal = parseFloat(form2.thetotal.value, 10).toFixed(2); alert(thetotal); alert value shows .00 but textbox value didn't show .00 – Twinxz May 18 '17 at 08:17
  • @Twinxz: check my answer below; `.toFixed()` works and can help you achieve what you want to do. – nyedidikeke May 18 '17 at 18:18

1 Answers1

0

Find below a snippet using toFixed() to demonstrate what you are trying to achieve; you can expand on that with your actual logics.

(function add() {
  document.getElementById('other_donation_check').addEventListener('click', function() {
    var this_chk_bx = document.getElementById('other_donation_check').checked;
    var amount_one = 2,
      amount_two = 250;
    if (this_chk_bx === true) {
      console.log('is checked');
      var thetotal = eval(amount_two + amount_one);
      console.log(thetotal.toFixed(2));
    } else {
      console.log('is not checked');
      var thetotal = eval(amount_two + amount_one);
      console.log(thetotal);
    }
  });
}());
<p>Check / uncheck the checkbox below to see the code in action.</p>
<input type="checkbox" id="other_donation_check">
nyedidikeke
  • 6,899
  • 7
  • 44
  • 59