Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating Check Boxes? 1

Status
Not open for further replies.

Rainman1234

Programmer
Nov 9, 2004
15
US
Hello All!

I am creating a form. (Atleast trying too!) On one spot on the form I have check boxes. You have 6 selections, but you can only choose 1. You also have 13 lines. On the 14th line they want the totals for the check boxes selected. I have exported all the values 1 through 6. (Column 1 = 1, Column 2 = 2 ...)I am having difficulty with the syntax. I have never used JavaScript before.
Here is my code so far:

if line1M.value = 1
TotalM1.value = TotalM1.value + 1;
if line1M.value = 2
TotalM2.value = TotalM2.value + 1;

'I would do the above code for colums 1-6 and lines 1-13.

I appreciate all inputs!


 
Look at this and let me know if you have more questions...

thread216-965713

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
MWolf00,

First let me thank you for responding. I am still having difficulty. Below is my entire code for 1 total column (TotalM1 is the name). I refuse to do the rest until I can get past this.


<script>
function calculate(injuryTotals) {
if line1M.value = 1 {
TotalM1.value = TotalM1.value + 1 }
if line2M.value = 1 {
TotalM1.value = TotalM1.value + 1 }
if line3M.value = 1 {
TotalM1.value TotalM1.value + 1 } if line4M.value = 1 {
TotalM1.value TotalM1.value + 1 }
if line5M.value = 1 {
TotalM1.value TotalM1.value + 1 }
if line6M.value = 1 {
TotalM1.value TotalM1.value + 1 }
if line7M.value = 1 {
TotalM1.value TotalM1.value + 1 }
if line8M.value = 1 {
TotalM1.value TotalM1.value + 1 }
if line9M.value = 1 {
TotalM1.value TotalM1.value + 1 }
if line10M.value = 1 {
TotalM1.value TotalM1.value + 1 }
if line11M.value = 1 {
TotalM1.value TotalM1.value + 1 }
if line12M.value = 1 {
TotalM1.value TotalM1.value + 1 }
if line13M.value = 1 {
TotalM1.value TotalM1.value + 1 }
}
</script>

 
<script>
function calculate(){
allInputs = document.getElementsByTagName("input")
ttlM1 = 0
for (x=0; x<allInputs.length; x++){
if (allInputs[x].type == "checkbox"){
//now your looking at each checkbox
//since this checkbox name ends w/ "M" we can check for that
boxName = allInputs[x].name
if (boxName.substr(boxName.length-2,1) == "M" && allInputs[x].checked) ttlM1 ++
}
}
}
</script>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
Try something like:

<script language="javascript">
function calculate(injuryTotals)
{
var grandtotal=0, checkboxlimit=13;
var el=document.forms['formname'].elements;

for (var ci=1;ci<=checkboxlimit;ci++)
{
if (el['line' + ci + 'M'].value ==1)
{
grandtotal += el['line' + ci + 'M'].value * 1;
}
}
el['TotalM1'].value = grandtotal;
}
</script>

You also send the argument injuryTotals to the function, but don't ever do anything with it.

You'll have to replace formname with whatever the name of your form is.

Lee
 
MWolf00 & Trollacious,

Thank you both for your inputs! I will try them both and let you know how it works out.

Thanks,

Rainman1234
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top