JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 7th, 2012, 11:15 AM
billybeaver billybeaver is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 billybeaver User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 6 m
Reputation Power: 0
Calculating total with discount

Hello,
i have a simple form that calculates the total of some checkboxes which works fine. Now, I need to offer a flat rate when 2 or more boxes are checked.
Example: 1 box = 100, 2 boxes or more = 175.
Finally, certain checkboxes must be exempt from the discount.
I am new to JS and just to get the form i currently have to work, took me forever, so please be gentle!
Here is my code:


<script type="text/javascript">
function calculate() {
var elems = document.forms['form1'].elements;
var total = 0;
for(var i=0;i<elems.length;i++) {
if (elems[i].checked) {total += +(elems[i].value);}
}
elems['total'].value = total;
}
</script>


boxes where 2 or more must = 175:

<input type="checkbox" name="checkbox_group_1[]" onclick="calculate()" value="100" title="Check here" />
<input type="checkbox" name="checkbox_group_2[]" onclick="calculate()" value="100" title="Check here" />
<input type="checkbox" name="checkbox_group_3[]" onclick="calculate()" value="100" title="Check here" />

exempt boxes:

<input type="checkbox" name="checkbox_group_4[]" onclick="calculate()" value="20" title="Check here" />
<input type="checkbox" name="checkbox_group_5[]" onclick="calculate()" value="20" title="Check here" />

thanks!

Reply With Quote
  #2  
Old December 7th, 2012, 02:53 PM
jonathansampson jonathansampson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Atlanta, GA
Posts: 14 jonathansampson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 22 m 47 sec
Reputation Power: 0
I think I understand your requirements. Try the following to see if it does what you're attempting. Be sure to read the comments carefully to understand what is going on.

javascript Code:
Original - javascript Code
  1. // Provide an indexOf function if none exists
  2. if (!Array.prototype.indexOf) {
  3.     Array.prototype.indexOf = function (needle) {
  4.         for (var i = 0; i < this.length; i++) {
  5.             if (this[i] === needle) return i;
  6.         }
  7.         return -1;
  8.     };
  9. }
  10. // Run our code when the window is finished loading
  11. window.onload = function () {
  12.     // Reference to our form element, and other things
  13.     var form = document.forms["form1"], total = checked = 0,
  14.         exempt = ["checkbox_group_4[]", "checkbox_group_5[]"];
  15.     // Bind up our calculate method when our form changes
  16.     if (form.addEventListener) {
  17.         form.addEventListener("change", calculate, false);
  18.     } else if (form.attachEvent) {
  19.         form.attachEvent("onchange", calculate);
  20.     } else {
  21.         form.onchange = calculate;
  22.     }
  23.     // Define our Calculate function
  24.     function calculate () {
  25.         // Reset total and checked
  26.         total = 0, checked = 0;
  27.         // Cycle over each item in our form
  28.         for (var i = 0; i < form.length; i++) {
  29.             // Handle only those that are checked
  30.             if (form[i].checked) {
  31.                 // Add their value to the total value
  32.                 total += parseInt(form[i].value, 10);
  33.                 // If they aren't extempt from flatrate
  34.                 if (exempt.indexOf(form[i].name) < 0) {
  35.                     // Increment the number of eligible items
  36.                     checked += 1;
  37.                 }
  38.             }
  39.         }
  40.         // If more than one eligible item is checked
  41.         if (checked > 1) {
  42.             total = 175;
  43.         // Else if only one eligible item is checked
  44.         } else if (checked > 0) {
  45.             total = 100;
  46.         }
  47.         // Show the user the estimated value
  48.         form["total"].value = total;
  49.     }                         
  50. };


Note that I've removed your `onclick` attributes and am instead listening for any change events on the form itself. Anytime an element within the form changes, that event bubbles up to the form itself and that is where we respond:

html Code:
Original - html Code
    <form name="form1">     <input type="checkbox" name="checkbox_group_1[]" value="100" />     <input type="checkbox" name="checkbox_group_2[]" value="100" />     <input type="checkbox" name="checkbox_group_3[]" value="100" />     <input type="checkbox" name="checkbox_group_4[]" value="20"  />     <input type="checkbox" name="checkbox_group_5[]" value="20"  />     <br/>     <input type="text" name="total" /> </form>


View Online: jsfiddle.net/mX5qN/

Reply With Quote
  #3  
Old December 10th, 2012, 09:18 AM
billybeaver billybeaver is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 billybeaver User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 6 m
Reputation Power: 0
hey,
thanks alot for your help, the first part about the flat rate works great.
However there is a slight problem with the exempted boxes:

what works:
-when only an exempted checkbox is checked, the value is added to total.

Not working:
-when 1 exempted and 1 eligible are checked, only the eligible is calculated, when a second eligible is checked, the flat rate kicks in but the exempted is still not added to total.

In other words the exempted need to add to the total no matter what.

Well, thanks again, i would never been able to get this far on my own.

Reply With Quote
  #4  
Old December 10th, 2012, 09:33 AM
jonathansampson jonathansampson is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Atlanta, GA
Posts: 14 jonathansampson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 22 m 47 sec
Reputation Power: 0
I'm a little confused about your requirements.

When should the user pay the cost associated with clicking any of the "exempt" checkboxes?

Reply With Quote
  #5  
Old December 10th, 2012, 09:51 AM
billybeaver billybeaver is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 5 billybeaver User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 6 m
Reputation Power: 0
Hello,
sorry for being unclear.
The exempted box values need to be added to the total regardless of how many, or if any, of the eligible boxes are checked.
In other words they must not be affected by the flat rate discount.
So if 1 eligible box is checked and 1 exempted is checked, the total would be 120 (100+20)

If 2 eligible boxes are checked, the flat rate would make the total 175, but checking an exempted box would then add it's value (20) to the total making it 195.
Hope that makes more sense.
thanks

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Calculating total with discount

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap