473,503 Members | 2,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript and asp radio button array

1 New Member
Hi, can anyone help here?
I have the following code generated from a database, I want to have javascript calculate the

costs of the selected items using radio buttons, subtotal the costs and grandtotal the costs

ready for the selected items to be inserted back to the database. I did something like this

before with Checkboxes, but Radio button have to be named the same to maintain their

groupings.

Radio Button index is generated from the recordset using asp
Expand|Select|Wrap|Line Numbers
  1. <td>
  2. <%=(EquiptRS.Fields.Item("EquiptDescr").Value)%>
  3.         <input name="descr1" type="hidden" id="descr1" 
  4.  
  5. value="<%=(EquiptRS.Fields.Item("EquiptDescr").Value)%>" />
  6.         <input name="quantity<%=(EquiptRS.Fields.Item("EquipmentID").Value)-1%>" 
  7.  
  8. type="hidden" id="quantity<%=(EquiptRS.Fields.Item("EquipmentID").Value)-1%>" value="1.00" 
  9.  
  10. /></td>
  11.     <td><input name="price<%=(EquiptRS.Fields.Item("EquipmentID").Value)-1%>" type="radio" 
  12.  
  13. onClick="total(this.form,<%=(EquiptRS_total)%>)" value="<%= 
  14.  
  15. FormatNumber((EquiptRS.Fields.Item("day1").Value), 2, -2, -2, -2) %>" />
  16.         <%= FormatNumber((EquiptRS.Fields.Item("day1").Value), 2, -2, -2, -2) %>
  17. </td>
  18.  
  19. This creates an array from the EquipmentID and RS RecordCount from the database
  20.  
  21. Can I use id="DifferentName" to name="price[andgeneratedname]"? do i use GetElementByID to 
  22.  
  23. check if the element is checked or not?
  24. If I name all "name" elements the same I get NaN in the subtotals;
  25. Here is my code:
  26.  
  27. <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
  28. //-->
  29. function roundoff(amount) {
  30. return (amount == Math.floor(amount)) ? amount + '.00' : ( (amount*10 == 
  31.  
  32. Math.floor(amount*10)) ? amount + '0' : amount);
  33. }
  34.  
  35. function total(what,number) {
  36. var grandTotal = 0;
  37.  
  38. for (var i=0;i<number;i++) {
  39. if (what.elements['price' + i].checked)
  40. what.elements['price' + i].checked;
  41.  
  42.  
  43. what.elements['subtotal' + i].value=roundoff((what.elements['quantity' + i].value - 0) * 
  44.  
  45. (what.elements['price' + i].value - 0));
  46. if (what.elements['quantity' + i].value == "0")
  47. what.elements['subtotal' + i].value = "0.00";
  48.  
  49. subtotal=roundoff(what.elements['subtotal' + i].value)
  50. grandTotal += (what.elements['price' + i].value - 0) * (what.elements['quantity' + i].value 
  51.  
  52. - 0);
  53.  
  54. }
  55. //write the totals into the what.elements
  56. what.subtotal = roundoff(Math.round(subtotal*Math.pow(10,2))/Math.pow(10,2));
  57. what.grandTotal.value = roundoff(Math.round(grandTotal*Math.pow(10,2))/Math.pow(10,2));
  58.  
  59. }
  60. </SCRIPT>
  61.  
  62. Here is the HTML generated from the database:
  63.  
  64. <form id="addrange" name="addrange">
  65. <table width="100%" border="0" cellpadding="3">
  66.   <tr>
  67.  
  68.     <td width="19%">&nbsp;</td>
  69.     <td width="4%">Qty</td>
  70.     <td width="9%">1 Day    </td>
  71.     <td width="9%">2 Days    </td>
  72.     <td width="9%">3 Days</td>
  73.     <td width="9%">4 Days </td>
  74.     <td width="10%">5 Days </td>
  75.  
  76.     <td width="10%">6 Days</td>
  77.     <td width="9%">6+Days </td>
  78.     <td width="12%">subtotal</td>
  79.   </tr>
  80.  
  81.   <tr>
  82.     <td>Standard carving ski        </td>
  83.     <td><input name="quantity0" type="text" id="quantity0" value="1.00" size="2" /></td>
  84.     <td><input name="price0" id="pr1" type="radio" onclick="total(this.form,10)" 
  85.  
  86. value="8.50" />
  87.  
  88.         8.50</td>
  89.     <td><input name="price0" id="pr2" type="radio" onclick="total(this.form,10)" 
  90.  
  91. value="16.00"  />
  92.         16.00</td>
  93.     <td><input name="price0" id="pr3" type="radio" value="23.00" 
  94.  
  95. onclick="total(this.form,10)" />
  96.         23.00</td>
  97.     <td><input name="price0" id="pr4" type="radio" value="30.00" 
  98.  
  99. onclick="total(this.form,10)" />
  100.         30.00</td>
  101.  
  102.     <td><input name="price0" id="pr5" type="radio" value="37.00" 
  103.  
  104. onclick="total(this.form,10)" />
  105.         37.00</td>
  106.     <td><input name="price0" id="pr6" type="radio" value="42.00" 
  107.  
  108. onclick="total(this.form,10)" />
  109.         42.00</td>
  110.     <td>7.00</td>
  111.     <td><input name="subtotal0" type="text" id="subtotal0" value="0.00" size="5" /></td>
  112.   </tr>
  113.  
  114.  
  115.   <tr>
  116.     <td>Snow blade        </td>
  117.     <td><input name="quantity1" type="text" id="quantity1" value="1.00" size="2" /></td>
  118.     <td><input name="price1" id="pr1" type="radio" onclick="total(this.form,10)" 
  119.  
  120. value="8.50" />
  121.         8.50</td>
  122.     <td><input name="price1" id="pr2" type="radio" onclick="total(this.form,10)" 
  123.  
  124. value="16.00"  />
  125.         16.00</td>
  126.     <td><input name="price1" id="pr3" type="radio" value="23.00" 
  127.  
  128. onclick="total(this.form,10)" />
  129.  
  130.         23.00</td>
  131.     <td><input name="price1" id="pr4" type="radio" value="30.00" 
  132.  
  133. onclick="total(this.form,10)" />
  134.         30.00</td>
  135.     <td><input name="price1" id="pr5" type="radio" value="37.00" 
  136.  
  137. onclick="total(this.form,10)" />
  138.         37.00</td>
  139.     <td><input name="price1" id="pr6" type="radio" value="42.00" 
  140.  
  141. onclick="total(this.form,10)" />
  142.         42.00</td>
  143.  
  144.     <td>7.00</td>
  145.     <td><input name="subtotal1" type="text" id="subtotal1" value="0.00" size="5" /></td>
  146.   </tr>
  147.  
  148.   <tr>
  149.     <td>Superior carving ski        </td>
  150.     <td><input name="quantity2" type="text" id="quantity2" value="1.00" size="2" /></td>
  151.     <td><input name="price2" id="pr1" type="radio" onclick="total(this.form,10)" 
  152.  
  153. value="14.00" />
  154.         14.00</td>
  155.  
  156.     <td><input name="price2" id="pr2" type="radio" onclick="total(this.form,10)" 
  157.  
  158. value="27.00"  />
  159.         27.00</td>
  160.     <td><input name="price2" id="pr3" type="radio" value="36.50" 
  161.  
  162. onclick="total(this.form,10)" />
  163.         36.50</td>
  164.     <td><input name="price2" id="pr4" type="radio" value="46.50" 
  165.  
  166. onclick="total(this.form,10)" />
  167.         46.50</td>
  168.     <td><input name="price2" id="pr5" type="radio" value="55.00" 
  169.  
  170. onclick="total(this.form,10)" />
  171.  
  172.         55.00</td>
  173.     <td><input name="price2" id="pr6" type="radio" value="63.00" 
  174.  
  175. onclick="total(this.form,10)" />
  176.         63.00</td>
  177.     <td>10.50</td>
  178.     <td><input name="subtotal2" type="text" id="subtotal2" value="0.00" size="5" /></td>
  179.   </tr></table></form>
  180.  
In order for the radio buttons to work I name the buttons in their groups (6 buttons) the

same, but then all subtotals show NAN.
If I uniquely name the radio buttons (but interestingly leave the last one as is) the result

in that rows subtotal box returns the last ones value correctly,but then the radio buttons

dont work correctly.



Hope you can help.
Oct 26 '06 #1
1 6842
iam_clint
1,208 Recognized Expert Top Contributor
you can give each one a unique ID and request it by document.getElementById("id")
Oct 26 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
6508
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
6
1619
by: Andy | last post by:
I'm not sure why my checkbox code is not working as per intended. It always keeps saying "Please select the department" even though I check the department... appreciate any help. <!DOCTYPE HTML...
10
1871
by: Marshall Dudley | last post by:
I am wanting to deteremine which radio button is selected on a form and am using: //alert(document.CC.Ecom_Payment_Card_Type.length) for (var i=0;i<document.CC.Ecom_Payment_Card_Type.length;i++)...
3
2056
by: Amelyan | last post by:
When we want radio button to belong to a group name we say, radio1.GroupName="GroupA". In this case, radio1 will be unselected if another radio button is selected in "GroupA". Is there a way...
1
1442
by: number1.email | last post by:
Hello, I have a simple Web Page Questionairre in which questions are read from a database, and the user can indicate the correct answer via either a radio input control or a dropdown list. The...
24
6285
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
10
6050
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio...
4
68108
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
5
3654
by: plsHelpMe | last post by:
How to create dynamic javascript arrays using dojo toolkits Hello frens, I am in a big trouble. My objective is: I am having some categories shown by differnent radio buttons, on the click of...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.