finding end result with checkboxs and radio buttons in one calculation

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

finding end result with checkboxs and radio buttons in one calculation

 
0
  #1
Feb 27th, 2009
Hi there

Having trouble calculating the final cost of choices made by a user. I have 4 radio buttons and 2 checkboxes. The first three radio buttons are, all intensive purposes, chocolates with the last radio button stating that the user doesn't want any of the chocolates. The two checkboxes are a milkshake and candyfloss respectively.

The user may only choose one of the three chocolates and optionally the milkshake, candyfloss or both.

What I have so far is as follows:

Javascript code
  1. function calc(sec,val,item)
  2. {
  3.  
  4. if(!item) {
  5. var item = null;
  6. }
  7.  
  8.  
  9. switch(sec)
  10. {
  11.  
  12. case 1:
  13.  
  14. some code for a different function.
  15.  
  16. break
  17.  
  18. case 2:
  19.  
  20. cValue = parseInt(document.getElementById("purchaseTotal").value);
  21.  
  22. if(item.checked){
  23. document.getElementById("purchaseTotal").value = cValue+val;
  24. }else{
  25. document.getElementById("purchaseTotal").value = cValue-val;
  26. }
  27.  
  28. break;
  29. }
  30. }

html
  1. <td>Chocolate 1: ZAR 5.00</td>
  2. <td><input name="chocolate" type="radio" id="chocolate" value="" onClick="calc(2,5.00, this);">
  3. </td>
  4. <td>&nbsp;</td>
  5. </tr>
  6. <tr>
  7. <td>Chocolate 2: ZAR 5.00</td>
  8. <td><input name="chocolate" type="radio" id="chocolate" value="" onClick="calc(2,5.00, this);">
  9. </td>
  10. <td>&nbsp;</td>
  11. </tr>
  12. <tr>
  13. <td>Chocolate 3: ZAR 1 200.00</td>
  14. <td><input name="chocolate" type="radio" id="chocolate" value="" onClick="calc(2,5.00, this);">
  15. </td>
  16. <td>&nbsp;</td>
  17. </tr>
  18. <tr>
  19. <td>I do not wish to buy a chocolate.</td>
  20. <td><input name="chocolate" type="radio" id="chocolate" value="" onClick="calc(2,0, this);">
  21. </td>
  22. <td>&nbsp;</td>
  23. </tr>
  24. <tr>
  25. <td>Milkshake: ZAR 13.00</td>
  26. <td><input type="checkbox" name="milkshake" id="milkshake" value="" onClick="calc(2,13.00, this);"></td>
  27. <td>&nbsp;</td>
  28. </tr>
  29. <tr>
  30. <td>Candyfloss: ZAR 8.00</td>
  31. <td><input type="checkbox" name="candyfloss" id="candyfloss" value="" onClick="calc(2,8.00, this);"></td>
  32. <td>&nbsp;</td>
  33. </tr>
  34. <tr>
  35. <td colspan="3" align="right" valign="top">Total ZAR <input name="purchaseTotal" type="text" id="purchaseTotal" value="0"></td>

What happens at the moment is that clicking on one radio button doesn't subtract the amount from the previous selected radio button and clicking on the same radio button just keeps adding its value to the total. I'd like it to do the opposite and not add to the total if clicked repeatedly and if another radio button is selected the amount of the previous radio button must be deducted from the total.
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: finding end result with checkboxs and radio buttons in one calculation

 
0
  #2
Feb 27th, 2009
I managed to solve my own problem although it isn't the most elegant method. Code is below:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function calc(sec,val,item)
  2. {
  3.  
  4. if(!item) {
  5. var item = null;
  6. }
  7.  
  8.  
  9. switch(sec)
  10. {
  11.  
  12. case 1:
  13.  
  14. some code for a different function.
  15.  
  16. break
  17.  
  18. case 2:
  19.  
  20. cValue = parseInt(document.getElementById("purchaseTotal").value);
  21.  
  22. if(item.checked){
  23. document.getElementById("purchaseTotal").value = cValue+val;
  24. }else{
  25. document.getElementById("purchaseTotal").value = cValue-val;
  26. }
  27.  
  28. break;
  29.  
  30. case 3:
  31.  
  32. cValue = parseInt(document.getElementById("confRegFeeT").value);
  33.  
  34. if(item.checked){
  35. if(val==5){
  36. if((cValue>=5) || (cValue>=26)){
  37. document.getElementById("confRegFeeT").value = cValue;
  38. }else{
  39. document.getElementById("confRegFeeT").value = cValue+val;
  40. }
  41. }else if(val==0){
  42. if(cValue<=21){
  43. document.getElementById("confRegFeeT").value = cValue;
  44. }else{
  45. document.getElementById("confRegFeeT").value = cValue-5;
  46. }
  47. }else{
  48. document.getElementById("confRegFeeT").value = cValue;
  49. }
  50. }
  51.  
  52. break;
  53. }
  54. }

In my html I changed the onclick for the radio buttons to calc(3,5,this)
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC