473,461 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Assigning values to check boxes and adding the numbers

The following routine adds a number to the total each time the checkbox with an assigned number is checked. This works perfectly (Example 1). But when I try to assign a variable that was calculated previously (Example 2), it returns a NaN in the Total, Can someone help me fix this? Thanks.

Example 1. This works
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     function checkTotal() {
  3.         document.listForm.total.value = '';
  4.         var sum = 0;
  5.         for (i=0;i<document.listForm.choice.length;i++) {
  6.           if (document.listForm.choice[i].checked) {
  7.               sum = sum + parseInt(document.listForm.choice[i].value);
  8.           }
  9.         }
  10.         document.listForm.total.value = sum;
  11.     }
  12. </script>
  13.  
  14. <form name="listForm">
  15. <input type="checkbox" name="choice" value="2" onchange="checkTotal()"/>2<br/>
  16. <input type="checkbox" name="choice" value="5" onchange="checkTotal()"/>5<br/>
  17. <input type="checkbox" name="choice" value="10" onchange="checkTotal()"/>10<br/>
  18. <input type="checkbox" name="choice" value="20" onchange="checkTotal()"/>20<br/>
  19. Total: <input type="text" size="2" name="total" value="0"/>
  20. </form>
  21.  
Example 2. This returns a NaN in the Total field
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     function checkTotal() {
  3.         document.listForm.total.value = '';
  4.         var sum = 0;
  5.         for (i=0;i<document.listForm.choice.length;i++) {
  6.           if (document.listForm.choice[i].checked) {
  7.               sum = sum + parseInt(document.listForm.choice[i].value);
  8.           }
  9.         }
  10.         document.listForm.total.value = sum;
  11.     }
  12. </script>
  13.  
  14. <form name="listForm">
  15. <input type="text" name="Item1cost" value="2" >2
  16. <input type="checkbox" name="choice" value="Item1cost" onchange="checkTotal()"/><br>
  17.  
  18. <input type="text" name="Item2cost" value="5" >5
  19. <input type="checkbox" name="choice" value="Item2cost " onchange="checkTotal()"/><br>
  20.  
  21. <input type="text" name="Item3cost " value="5" >5 
  22. <input type="checkbox" name="choice" value="Item3cost" onchange ="checkTotal()"/> <br>
  23.  
  24. <input type="test" name="Item4cost" value="20">20
  25. <input type="checkbox" name="choice" value="Item4cost" onchange="checkTotal()"/> <br>
  26.  
  27. Total: <input type="text" size="4" name="total" value="0"/>
  28. </form>
  29.  
Nov 19 '11 #1

✓ answered by Dormilich

taking the snippet from above:
Expand|Select|Wrap|Line Numbers
  1. var box = document.getElementsByName("choice")[0];
  2. alert(box.value);
  3. var txt = document.getElementsByName(box.value)[0];
  4. alert(txt.value);
  5.  
  6. // or
  7. var box = document.listForm.choice[0];
  8. var txt = document.listForm[box.value]; // since there is only one such name
  9. alert(txt.value);

5 3761
Dormilich
8,658 Expert Mod 8TB
it’s a wrong assumption that you can in the value reference a number from another element.
Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="Item1cost" value="2" >2
  2. <input type="checkbox" name="choice" value="Item1cost" onchange="checkTotal()"/>
the value of the checkbox is the string "Item1cost" which obviously is parsed as NaN.

what you would need to do is taking the value from the checkbox and using that for fetching the value of the element that has this ID/name.
Nov 20 '11 #2
I'm sorry, I don't understand. Can you give me an example? Thanks.
Nov 20 '11 #3
Dormilich
8,658 Expert Mod 8TB
taking the snippet from above:
Expand|Select|Wrap|Line Numbers
  1. var box = document.getElementsByName("choice")[0];
  2. alert(box.value);
  3. var txt = document.getElementsByName(box.value)[0];
  4. alert(txt.value);
  5.  
  6. // or
  7. var box = document.listForm.choice[0];
  8. var txt = document.listForm[box.value]; // since there is only one such name
  9. alert(txt.value);
Nov 21 '11 #4
Thanks so much. It makes so much sense once you showed me how to do it..
Nov 21 '11 #5
Well it wasn't easy for someone who doesn't know Javascript, nor is it intuitive. I had to deal with parsing the text that converted it to numbers. For anyone wanting to do this, here's the code with the help of "Dormillich". It still took me half a day to figure it out.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function checkTotal() {
  5.     document.listForm.total.value = '';
  6.     var sum = 0;
  7.     for (i=0;i<document.listForm.choice.length;i++) {
  8.          if (document.listForm.choice[i].checked) {
  9.     var box = (document.listForm.choice[i]);
  10.         alert(box.value);
  11.     var txt = (document.listForm[box.value]); 
  12.            alert(txt.value);
  13.     sum = sum + parseInt(txt.value) ;    
  14.            alert(sum);
  15.            }
  16.     }
  17.      document.listForm.total.value = sum;
  18. }
  19. </script>
  20.  
  21. <form name="listForm">
  22. <input type="text" name="Item1cost" value="2" >2
  23. <input type="checkbox" name="choice" value="Item1cost" onChange="checkTotal()"/><br>
  24.  
  25. <input type="text" name="Item2cost" value="5" >5
  26. <input type="checkbox" name="choice" value="Item2cost" onChange="checkTotal()"/><br>
  27.  
  28. <input type="text" name="Item3cost" value="5" >5 
  29. <input type="checkbox" name="choice" value="Item3cost" onchange ="checkTotal()"/> <br>
  30.  
  31. <input type="test" name="Item4cost" value="20">20
  32. <input type="checkbox" name="choice" value="Item4cost" onChange="checkTotal()"/> <br>
  33.  
  34. Total: <input type="text" size="10" name="total" value="0"/>
  35. </form>
  36. </body>
  37. </html>
  38.  
Nov 22 '11 #6

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

Similar topics

13
by: Adrian Parker | last post by:
I have a PHP generated page which displays X many records. Each record has a checkbox preceding it. The user checks several checkboxes, and hits a delete button. All the corresponding records...
2
by: Ben | last post by:
My current project requires me to create part of a form that is created on the fly. The project consists a list of entries to an event. The name and address and such is easy. The design is detup so...
14
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0;...
5
by: Shreekant Patel | last post by:
Hello, I am new to the advanced level of access, and I have quite a few changes to make to an existing database. The current database's main form needs to have additional check boxes added to...
2
by: cpptutor2000 | last post by:
Could some PHP guru please help me? I have very standard PHP - MySQL application that reads in some data from a table and for each row, puts a check box at the start of the row. Now the check boxes...
4
by: kitesurfer | last post by:
Looking for some help with an email form using php. I am using check boxes in a html form, the values of check boxes are email addresses. The person visiting our site has the ability to select...
2
by: vibee | last post by:
this might be a simple question but how do i assign values to a check box in a query condition, i have the following so far: Required: IIf(="existing",True,False) The problem is the check boxes...
11
by: Patrick | last post by:
Trying this question again in a different way and expanding it to another newsgroup. Looking for how I would do this. For an html form; Say I have three check boxes A, B, and C . When I click...
1
by: ghjk | last post by:
my php page has 7 check boxes. I stored checked values to database and retrive as binary values. This is the result array Array ( => 0 => 1 => 0 => 1 => 0 => 0 => 1 ) 1 means checked....
2
by: Desi | last post by:
Hello, I have to make a burger system on Visual Basic. I have table numbers in a combo box, burgre types in a com box and bun types also in a combo box. And side orders like olives,salad are in...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.