Hello guys, i'm trying to calculate the value of checkbox that chosen (onclick or onsubmit calculate the value).What i'm doing wrong?

<script type="text/javascript">
//<![CDATA[
function calculate_total(id) {
   // get a reference to the form using getElementById
   var theForm = document.getElementById( id ),
       total = 0;
   // DON'T use getElementById again, use the form element names, like this:-
   if (theForm.DigRacing.checked) {
      total += parseFloat(theForm.DigRacing.value);
   }
   if (theForm.AtariJoystick.checked) {
      total += parseFloat(theForm.AtariJoystick.value);
   }
   if (theForm.FXLightsaber.checked){
	  total += parseFloat(theForm.FXLightsaber.value); 
	   
	   }
	  

}

</script>


</head>
<body>

<form name ="orderForm" action="#" id="orderForm" >
<div class="advert">
  <br />&pound;99.95 <strong>Add to Cart</strong>
  <input type="checkbox" name="DigRacing" value="99.95" />

</div>
<div class="advert">


  <br />&pound;19.99 <strong>Add to Cart</strong>
  <input type="checkbox" name="AtariJoystick" value="19.99"  />
</div>
<div class="advert">
  <br />&pound;299.95 <strong>Add to Cart</strong>
  <input type="checkbox" name="FXLightsaber" value="299.95" />
</div>
<br />

<p><input type="button" name="CheckValue" value = "Calculate cost"  onclick="calculate_total(orderForm, total)" />
</form>

</body>
</html>

You are using the exact same form element name for the checkbox and the textboxes:

if (theForm.DigRacing.checked) {
   total += parseFloat(theForm.DigRacing.value);
}
if (theForm.AtariJoystick.checked) {
   total += parseFloat(theForm.AtariJoystick.value);
}
if (theForm.FXLightsaber.checked){
   total += parseFloat(theForm.FXLightsaber.value); 
}

Shouldn't they be different?

What kind of errors are you getting?

I got error on the first line here "theForm is null".

if (theForm.DigRacing.checked) {
total += parseFloat(theForm.DigRacing.value);
}

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.