...

View Full Version : How can I use an onclick event to calculate figures in form elements?



pml
03-11-2005, 06:50 PM
I have some problems with these objects. I want the checked items to be added up and the sum displayed at the end. Unfortunately I don't have a good "Calculate" function for this purpose and hope maybe you've got an idea about it. The code should preferebly be dynamic, e.g. the code shouldn't mind whether there is four objects (as in this case) or 15 more radio groups and an additional 10 checkboxes. When the URL is loaded the default sum should also be displayed (4). Is it possible to achieve that?

<html>
<head>

<script language="javascript" type="text/javascript">
function Calculate (number) {


}
</script>
</head>

<body>
<p>

<input name="RadioGroup1" type="radio" value="SomeValue1" checked onClick="Calculate(1)"> 1 <br>
<input name="RadioGroup1" type="radio" value="SomeValue2" onClick="Calculate(2)"> 2 <br>
<input name="RadioGroup1" type="radio" value="SomeValue3" onClick="Calculate(3)"> 3 <br>
<input name="check1" type="checkbox" id="check1" value="SomeValue4" onClick="Calculate(8)"> 8</p>
<p>

<input name="RadioGroup2" type="radio" value="SomeValue5" checked onClick="Calculate(3)"> 3 <br>
<input name="RadioGroup2" type="radio" value="SomeValue6" onClick="Calculate(7)"> 7 <br>
<input name="RadioGroup2" type="radio" value="SomeValue7" onClick="Calculate(9)"> 9 <br>
<input name="check2" type="checkbox" value="SomeValue8" onClick="Calculate(12)"> 12 </p>
<p>The sum of the radio buttons and checkboxes is:
<script language="javascript" type="text/javascript">

document.write.??????????????


}
</script>
</p>


</body>
</html>

vwphillips
03-11-2005, 07:56 PM
not sure I understand but



<html>
<head>

<script language="javascript" type="text/javascript">
function Calculate (obj) {
ips=obj.parentNode.getElementsByTagName('INPUT');
sum=0;
for (i=0;i<ips.length;i++){
if (ips[i].checked){
sum+=ips[i].value*1;
}
}
document.getElementById('total').value=sum;
}
</script>
</head>

<body>
<p>
<input name="RadioGroup1" type="radio" value="1" onClick="Calculate(this)"> 1 <br>
<input name="RadioGroup1" type="radio" value="2" onClick="Calculate(this)"> 2 <br>
<input name="RadioGroup1" type="radio" value="3" onClick="Calculate(this)"> 3 <br>
<input name="check1" type="checkbox" id="check1" value="4" onClick="Calculate(this)"> 4
<br>
<br>
<input name="RadioGroup2" type="radio" value="5" onClick="Calculate(this)"> 5 <br>
<input name="RadioGroup2" type="radio" value="6" onClick="Calculate(this)"> 6 <br>
<input name="RadioGroup2" type="radio" value="7" onClick="Calculate(this)"> 7 <br>
<input name="check2" type="checkbox" value="8" onClick="Calculate(this)"> 8
</p>
<p>
The sum of the radio buttons and checkboxes is:<input id="total" size="10">
</p>


</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum