Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery subtract amount from sum of input fields when discount code entered
    primarykey
    data
    text
    <p>OK, I'm pulling my hair out over this. I have a form consisting of mainly drop-down selections and check boxes - these all have values assigned so as the user selects things, a total sum value of everything is calculated.</p> <p>My last input field is a text input called 'Discount Code' I want to be able to enter a set code and that subtract a fixed amount from the sum. </p> <p>Below is the script that I'm using to detect if the discount code is correct:</p> <pre><code>$(".apply_discount").click(function() { if ($("input[name='discount']").val() === "DISTR50") { $("span").text("Validated...").show(); $("input[name='discount']").attr("value",500.99); return true; } $("span").text("Not a valid discount code").show().fadeOut(2000); return false; }); </code></pre> <p>and here is the code which calculates the sum of my inputs:</p> <pre><code>//iterate through each textboxes and add keyup //handler to trigger sum event $(".txt, .select, .checkbox").each(function() { $(this).change(function(){ calculateSum(); }); }); }); function calculateSum() { var discount = $("input[name='discount']").attr('value'); var sum = 0; //iterate through each textboxes and add the values $(".txt, .select, .checkbox:checked").each(function() { //add only if the value is number if(!isNaN(this.value) &amp;&amp; this.value.length!=0) { sum += parseFloat(this.value); } }); //.toFixed() method will roundoff the final sum to 2 decimal places var calc_total = sum; $("#sum").html(calc_total.toFixed(2)); } </code></pre> <p>as you can see, the sum is appended to a div called #sum - I can't figure out how I subtract the value of the discount input.</p> <p>you can see my code in action here: <a href="http://www.samskirrow.com/projects/distr/index3.html" rel="nofollow">http://www.samskirrow.com/projects/distr/index3.html</a></p> <p>Here it is in a JSfiddle (calculate mileage section doesn't work though) <a href="http://jsfiddle.net/bZhK4/" rel="nofollow">http://jsfiddle.net/bZhK4/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload