0

I have a script that works pretty good for calculating a price total from checkboxes with a starting price. I got most of this code from Stackoverflow, but I need to tweak it a little.

I would like to add a comma to separate thousands in the total dollar amount. I've seen a few functions for this but don't know where to add the thousands comma function.

Here is the code:

HTML

    <form action="#" method="" name="form">
    <fieldset>
    <div class="build-option">
    <div class="build-title">King-Dome</div> 
    <label class="build-label"><input type="checkbox" name="nozzle" value="1500"> $1,500</label>
    </div>
    <div class="build-option">
    <div class="build-title">Solar Package</div> 
    <label class="build-label"><input type="checkbox" name="nozzle" value="1800"> $1,800</label>
    </div>
    <div class="build-option">
    <div class="build-title">Additional Awning</div> 
    <label class="build-label"><input type="checkbox" name="nozzle" value="900"> $900</label>
    </div>
    <div class="build-option">
    <div class="build-title">Generator Basket</div> 
    <label class="build-label"><input type="checkbox" name="nozzle" value="250"> $250</label>
    </div>
    </fieldset>

Javascript

     <script type="text/javascript">
    window.onload=function() {
    document.getElementsByTagName('fieldset')[0].onclick = totalizer;
    };

    function totalizer(e) {
    var e = e? e : window.event;
    var target = e.srcElement || e.target;
    if(e.type=='click' && target.type=='checkbox') { 
    var cost = document.getElementById('cost');
    var extra = parseFloat(target.value);
    cost.firstChild.data = (parseFloat(cost.firstChild.data) + ((target.checked)? extra : -extra)).toFixed(2);

    var parts = e.toString().split("."); //add this for comment - didn't work
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); //add this for comment - didn't work
     return parts.join("."); //add this for comment - didn't work

    }
    }
    </script>

I also need to find a script to clear form elements by click a "Clear" form button and a script that clears the form checks if the page is refreshed. I could possibly skip the clear button if I could get the script to calculate "already" checked boxes when refreshing the page.

If you check a box now, the total will update. But if you leave the check then refresh the page, the starting total is then changed )it should be $39950. If you uncheck the box that was checked before refreshing, the total is then less than what the starting total should be.

I may need to ask a another question for this second part. Main thing is to fix the comma issue first.

Matt Duncan
  • 43
  • 1
  • 11
  • This question has been asked before, take a look here: http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript – Sjiep Apr 15 '14 at 02:53
  • I have tried this but cannot get it to work with the totalizer function I already have input. I am probably not adding it correctly, but I'm no javascript guru either. – Matt Duncan Apr 15 '14 at 03:03
  • I updated the javascript trying to implement the var parts, but didn't work. You will see the three lines of code I added where you see comments. – Matt Duncan Apr 15 '14 at 03:11

0 Answers0