PDA

View Full Version : JavaScript Calculation Help?



jonsey
01-30-2007, 08:44 PM
New to programming here was wounding if you can do calculation with JavaScript. Trying to build a form I have about 4 radio buttons list with a “yes” and “no” labels and each “Yes” has a value of 10 and “No” with a value of 0. Is there a way to total up the “Yes” values into a single field?

Thanks in Advance
Jonsey



<input id="Radio1" type="radio" name="rblA" value="10"/>
Yes&nbsp;
<input id="Radio2" type="radio" name="rblA" value="0"/>
No<br />
<br />
<input id="Radio3" type="radio" name="rblB" value="10"/>
Yes&nbsp;
<input id="Radio4" type="radio" name="rblB" value="0"/>
No<br />
<br />
<input id="Radio5" type="radio" name="rblC" value="10"/>
Yes&nbsp;
<input id="Radio6" type="radio" name="rblC" value="0"/>
No<br />
<br />
<input id="Radio7" type="radio" name="rblD" value="10"/>
Yes&nbsp;
<input id="Radio8" type="radio" name="rblD" value="0"/>
No<br />
<br />
<br />

beeps
01-31-2007, 07:32 AM
Please try this...Hope this is what you wanted.

<html>
<head>
<title></title>
<meta NAME="Generator" CONTENT="TextPad 4.6">
<script language="javascript">
var allOptions = document.getElementsByTagName('input')
var totalScore = 0
function fSelectOption(lOption){
lOption.weight = lOption.value
}
function fShowResult(){
for(var i=0;i<allOptions.length;i++){
if(allOptions[i].checked){
totalScore+=Math.round(allOptions[i].weight)
}
}
document.getElementById('result').innerHTML="Total Score = "+totalScore
}
</script>
</head>

<body BGCOLOR="#FFFFFF">
<input id="Radio1" type="radio" name="rblA" value="10" onactivate="fSelectOption(this)"/>Yes&nbsp;<input id="Radio2" type="radio" name="rblA" value="0" onactivate="fSelectOption(this)"/>No<br /><br />
<input id="Radio3" type="radio" name="rblB" value="10" onactivate="fSelectOption(this)"/>Yes&nbsp;<input id="Radio4" type="radio" name="rblB" value="0" onactivate="fSelectOption(this)"/>No<br /><br />
<input id="Radio5" type="radio" name="rblC" value="10" onactivate="fSelectOption(this)"/>Yes&nbsp;<input id="Radio6" type="radio" name="rblC" value="0" onactivate="fSelectOption(this)"/>No<br /><br />
<input id="Radio7" type="radio" name="rblD" value="10" onactivate="fSelectOption(this)"/>Yes&nbsp;<input id="Radio8" type="radio" name="rblD" value="0" onactivate="fSelectOption(this)"/>No<br /><br />
<br />
<input type="button" value="submit" onclick="fShowResult()" />
<div id="result" style="position:absolute; width:190px; height:20px; z-index:1; left: 200; top: 150; visibility: visible;"></div>
</body>
</html>

jonsey
01-31-2007, 02:01 PM
That is it, your are the man/lady :) Just one thing is there a way to do that dynamic as each radio button is check?

beeps
02-01-2007, 05:07 AM
hey there...I am not able to get you...please gime me more specifications..

jonsey
02-01-2007, 01:59 PM
Ok for example when every the Yes and No radio buttons holds a weight value for the question. So when the Yes is mark then it would put that question weight value in the TotalResults but if No is mark it will put a 0 value in the TotalResults. This would all be done in real time. So ever time a Yes it mark it would fire off the Sum and total up the All the Radio marked Yes and if no I mark I would Refire the Total to get new value.

beeps
02-05-2007, 05:24 AM
Hi There,
please check this out....
------------------------------------------------------------
<html>
<head>
<title></title>
<meta NAME="Generator" CONTENT="TextPad 4.6">
<script language="javascript">
var allOptions = document.getElementsByTagName('input')
function fSelectOption(lOption){
lOption.weight = lOption.value
setTimeout("fShowResult()",100);
}
function fShowResult(){
var totalScore = 0
for(var i=0;i<allOptions.length;i++){
if(allOptions[i].checked){
totalScore+=Math.round(allOptions[i].weight)
}
}
document.getElementById('result').innerHTML="Total Score = "+totalScore
}
</script>
</head>

<body BGCOLOR="#FFFFFF">
<input id="Radio1" type="radio" name="rblA" value="10" onclick="fSelectOption(this);"/>Yes&nbsp;<input id="Radio2" type="radio" name="rblA" value="0" onclick="fSelectOption(this)"/>No<br /><br />
<input id="Radio3" type="radio" name="rblB" value="10" onclick="fSelectOption(this)"/>Yes&nbsp;<input id="Radio4" type="radio" name="rblB" value="0" onclick="fSelectOption(this)"/>No<br /><br />
<input id="Radio5" type="radio" name="rblC" value="10" onclick="fSelectOption(this)"/>Yes&nbsp;<input id="Radio6" type="radio" name="rblC" value="0" onclick="fSelectOption(this)"/>No<br /><br />
<input id="Radio7" type="radio" name="rblD" value="10" onclick="fSelectOption(this)"/>Yes&nbsp;<input id="Radio8" type="radio" name="rblD" value="0" onclick="fSelectOption(this)"/>No<br /><br />
<br />
<!--input type="button" value="submit" onclick="fShowResult()" /-->
<div id="result" style="position:absolute; width:190px; height:20px; z-index:1; left: 200; top: 150; visibility: visible;"></div>
</body>
</html>

jonsey
02-07-2007, 02:08 PM
That work great, thanks for you help with this Beeps :)