I have need of some help...i am working on a form in javascript and am wanting to create a function to total the value of checkboxes when they are checked. i have found many examples, and have a little bit done but it won't total up the values.

this is the function:

<script type="text/javascript" language="JavaScript">
//

// Function for Form F2

function checkTotal() {
		document.listForm.total.value = '';
		var sum = 0;
		for (i=0;i<document.listForm.choice.length;i++) {
		  if (document.listForm.choice[i].checked) {
		  	sum = sum + parseInt(document.listForm.choice[i].value);
		  }
		}
		document.listForm.total.value = sum;
	}

and the form that i have is:

</HEAD>
<BODY bgcolor="beige">

<form name="listForm">
<input type="checkbox" name="choice" value="30" onclick="javascript: checkTotal(listForm)"/>  (30.00)<br>
<input type="checkbox" name="choice" value="20" onclick="javascript: checkTotal(listForm)"/> (20.00)/each<br>
<input type="checkbox" name="choice" value="0"  onclick="javascript: checkTotal(listForm)"/> (free)<br>
<input type="checkbox" name="choice" value="0" onclick="javascript: checkTotal(listForm)"/> (free)<br>
<input type="checkbox" name="choice" value="12" onclick="javascript: checkTotal(listForm)"/>(free)<br>
Total: <input type="text" size="5" name="total" value="20"/>
</fieldset>

if i could get some help on where I have messed up i would appreciate it a ton!

thanks,

If this is the compleye code, you didn't close the <script> tag. Also just check whether you closed <form> and <body> tags.

If it is closed, it is working in my system.

i have another real quick question...do you have to do a script tag for each function?

No need.

Put all your functions in one <script> and </script> tags.

No need to pass argument to the javascript function.

<input type="checkbox" name="choice" value="30" onclick="checkTotal()"/> (30.00)<br>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.