Search code examples
javascriptformscalculated-field

How to replace form input field with calculation?


I have a page at http://www.galileomovement.com.au/tshirt_order2.php with a form which calculates order totals from user inputs and displays the total as well as a donation total. A $10 donation is built into the price but I want the buyer to be able to vary this amount.

It all works fine except that nothing happens when a buyer enters a donation amount. The relevant form fields are:

T-shirts total: $<input type="text" name="shirtsTotal" id="shirtsTotal" size="24" readonly class="extension" /><br />
Donation total: $<input type="text" name="donationTotal" id="donationTotal" size="6" readonly class="extension" /><br />
<input type="text" name="grandTotal" id="grandTotal" size="24" readonly class="extension" style="font-weight:bold;" /><br />
Change total donation to: $<input type="text" onkeyup="calctotal(this.form);" name="optionalDonation" id="changeDonation" size="7" />

In tshirt_total3.js I have:

// Get the total donation value
    var changeDonation = document.getElementById('changeDonation');
    if (changeDonation.value > 0){
        donationTotal = changeDonation.value;
    }
    else {
        var donation = 10;
        if(document.getElementById('donation_false').checked) {
            donation = 0;
        }
        for (i = 1; i <= 10; i++) {
            quantity = 'quantity' + i;
            var quantity = document.getElementById(quantity);
            donationTotal += quantity.value * donation;
        }
    }
    // end of Get the total donation value

Can anybody see where I have gone wrong? I've spent hours on searching for the answer.


Solution

  • Just checked your website I noticed that there is an error of undefined variable in your tshirt_total3.js at line number 60 which comes when i type something on the donation inputbox, maybe because of that your rest of the code is not executing