0

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>
2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by marios_neo
0

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?

0

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

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.