Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding the values of Radio buttons

Status
Not open for further replies.

PeachRL

Technical User
Nov 9, 2000
6
US
I am using radio buttons as a way to select items for purchase. How can I add their values and display the total in a msgbox?? All suggestions appreciated!
 
I believe you actually need checkboxes instead of radio buttons. This bit of example code is in javascript as it is more widely accepted by browsers...
Code:
<html>
<head>
<script language=&quot;javascript&quot;>
 function Calculate(){
  var Total = 0;
  var maxRadio = 3;

  for (var i=1;i<=maxRadio;i++){
   if (eval(&quot;document.forms.frmDemo.chk&quot; + i + &quot;.checked&quot;)==true){
    Total += parseFloat(eval(&quot;document.forms.frmDemo.chk&quot; + i + &quot;.value&quot;))
   }
  }
  alert(&quot;Your total is: $&quot; + Total)
 }
</script>
</head>
<body>
<form id=frmDemo>
 <input type=checkbox value=&quot;30.00&quot; id=chk1>Shirt: $30.00<br>
 <input type=checkbox value=&quot;47.50&quot; id=chk2>Pants: $47.50<br>
 <input type=checkbox value=&quot;65.37&quot; id=chk3>Silk Tie: $65.37<br><br> 
 <input type=button value=&quot;Calculate&quot; onClick=&quot;Calculate()&quot;>
</form>
</body>
</html>

There are other ways of doing this, but this one is probably the easiest to understand.

Later, Rob
robschultz@yahoo.com
-Focus on the solution to the problem, not the obstacles in the way.-
 
As requested. The javascript is left in so you can compare the two languages...

Code:
<html>
<head>
<script language=&quot;javascript&quot;>
 function Calculate(){
  var Total = 0;
  var maxRadio = 3;

  for (var i=1;i<=maxRadio;i++){
   if (eval(&quot;document.forms.frmDemo.chk&quot; + i + &quot;.checked&quot;)==true){
    Total += parseFloat(eval(&quot;document.forms.frmDemo.chk&quot; + i + &quot;.value&quot;))
   }
  }
  alert(&quot;Your total is: $&quot; + Total)
 }
</script>
<script language=&quot;vbscript&quot;>
 Sub vbCalculate()
  Dim Total, maxRadio, i
  Total = 0
  maxRadio = 3
  
  For i = 1 to maxRadio
   If Eval(&quot;document.forms.frmDemo.chk&quot; & i & &quot;.checked = True&quot;) Then
    Total = Total + eval(&quot;document.forms.frmDemo.chk&quot; & i & &quot;.value&quot;)
   End If
  Next
  
  Total = FormatCurrency(Total)
  MsgBox(&quot;Your total is: &quot; & Total)
 End Sub
</script>
</head>
<body>
<form id=frmDemo>
 <input type=checkbox value=&quot;30.00&quot; id=chk1>Shirt: $30.00<br>
 <input type=checkbox value=&quot;47.50&quot; id=chk2>Pants: $47.50<br>
 <input type=checkbox value=&quot;65.37&quot; id=chk3>Silk Tie: $65.37<br><br> 
 <input type=button value=&quot;Calculate&quot; onClick=&quot;vbCalculate()&quot;>
</form>
</body>
</html>

Later, Rob
robschultz@yahoo.com
-Focus on the solution to the problem, not the obstacles in the way.-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top