View Full Version : averaging selected values from radio button groups

07-06-2008, 12:35 PM
First off, I'd like to say I can't code to save my life; this is really just me begging for code rather than asking for help coding. What I want to do is pretty simple, and I could definitely see this being resolved in just a couple posts.

I have 8 fields of 10 radio buttons each with values from 0-10. I want a button that averages (adds and divides by 8) the values of all the buttons checked (one button checked per group, 8 groups). That is to say, what I want to happen is if I clicked 2 for group1, 8 for group2, 4 for group3, 5 for group4, 10 for group5, 9 for group6, 7 for group7 and 3 for group8, I would click Submit and in the textfield would appear the number 6.

<title>Mah buttons. They do nothing.</title>
<form id="form1" name="form1" method="post" action="">
<table width="100%" border="1">
<td><label><input name="RadioGroup1" type="radio" value="0" checked="checked" />0</label>
<br /><label><input type="radio" name="RadioGroup1" value="1" />1</label>
<br /><label><input type="radio" name="RadioGroup1" value="2" />2</label>
<br /><label><input type="radio" name="RadioGroup1" value="3" />3</label>
<br /><label><input type="radio" name="RadioGroup1" value="4" />4</label>
<br /><label><input type="radio" name="RadioGroup1" value="5" />5</label>
<br /><label><input type="radio" name="RadioGroup1" value="6" />6</label>
<br /><label><input type="radio" name="RadioGroup1" value="7" />7</label>
<br /><label><input type="radio" name="RadioGroup1" value="8" />8</label>
<br /><label><input type="radio" name="RadioGroup1" value="9" />9</label>
<br /><label><input type="radio" name="RadioGroup1" value="10" />10</label></td>
<td><label><input name="RadioGroup2" type="radio" value="0" checked="checked" />0</label>
<br /><label><input type="radio" name="RadioGroup2" value="1" />1</label>
<br /><label><input type="radio" name="RadioGroup2" value="2" />2</label>
<br /><label><input type="radio" name="RadioGroup2" value="3" />3</label>
<br /><label><input type="radio" name="RadioGroup2" value="4" />4</label>
<br /><label><input type="radio" name="RadioGroup2" value="5" />5</label>
<br /><label><input type="radio" name="RadioGroup2" value="6" />6</label>
<br /><label><input type="radio" name="RadioGroup2" value="7" />7</label>
<br /><label><input type="radio" name="RadioGroup2" value="8" />8</label>
<br /><label><input type="radio" name="RadioGroup2" value="9" />9</label>
<br /><label><input type="radio" name="RadioGroup2" value="10" />10</label></td>
<td><label><input name="RadioGroup3" type="radio" value="0" checked="checked" />0</label>
<br /><label><input type="radio" name="RadioGroup3" value="1" />1</label>
<br /><label><input type="radio" name="RadioGroup3" value="2" />2</label>
<br /><label><input type="radio" name="RadioGroup3" value="3" />3</label>
<br /><label><input type="radio" name="RadioGroup3" value="4" />4</label>
<br /><label><input type="radio" name="RadioGroup3" value="5" />5</label>
<br /><label><input type="radio" name="RadioGroup3" value="6" />6</label>
<br /><label><input type="radio" name="RadioGroup3" value="7" />7</label>
<br /><label><input type="radio" name="RadioGroup3" value="8" />8</label>
<br /><label><input type="radio" name="RadioGroup3" value="9" />9</label>
<br /><label><input type="radio" name="RadioGroup3" value="10" />10</label></td>
<td><label><input name="RadioGroup4" type="radio" value="0" checked="checked" />0</label>
<br /><label><input name="RadioGroup4" type="radio" value="1" />1</label>
<br /><label><input type="radio" name="RadioGroup4" value="2" />2</label>
<br /><label><input type="radio" name="RadioGroup4" value="3" />3</label>
<br /><label><input type="radio" name="RadioGroup4" value="4" />4</label>
<br /><label><input type="radio" name="RadioGroup4" value="5" />5</label>
<br /><label><input type="radio" name="RadioGroup4" value="6" />6</label>
<br /><label><input type="radio" name="RadioGroup4" value="7" />7</label>
<br /><label><input type="radio" name="RadioGroup4" value="8" />8</label>
<br /><label><input type="radio" name="RadioGroup4" value="9" />9</label>
<br /><label><input type="radio" name="RadioGroup4" value="10" />10</label></td>
<table width="100%" border="1">
<td><label><input name="RadioGroup5" type="radio" value="0" checked="checked" />0</label>
<br /><label><input type="radio" name="RadioGroup5" value="1" />1</label>
<br /><label><input type="radio" name="RadioGroup5" value="2" />2</label>
<br /><label><input type="radio" name="RadioGroup5" value="3" />3</label>
<br /><label><input type="radio" name="RadioGroup5" value="4" />4</label>
<br /><label><input type="radio" name="RadioGroup5" value="5" />5</label>
<br /><label><input type="radio" name="RadioGroup5" value="6" />6</label>
<br /><label><input type="radio" name="RadioGroup5" value="7" />7</label>
<br /><label><input type="radio" name="RadioGroup5" value="8" />8</label>
<br /><label><input type="radio" name="RadioGroup5" value="9" />9</label>
<br /><label><input type="radio" name="RadioGroup5" value="10" />10</label></td>
<td><label><input name="RadioGroup6" type="radio" value="0" checked="checked" />0</label>
<br /><label><input type="radio" name="RadioGroup6" value="1" />1</label>
<br /><label><input type="radio" name="RadioGroup6" value="2" />2</label>
<br /><label><input type="radio" name="RadioGroup6" value="3" />3</label>
<br /><label><input type="radio" name="RadioGroup6" value="4" />4</label>
<br /><label><input type="radio" name="RadioGroup6" value="5" />5</label>
<br /><label><input type="radio" name="RadioGroup6" value="6" />6</label>
<br /><label><input type="radio" name="RadioGroup6" value="7" />7</label>
<br /><label><input type="radio" name="RadioGroup6" value="8" />8</label>
<br /><label><input type="radio" name="RadioGroup6" value="9" />9</label>
<br /><label><input type="radio" name="RadioGroup6" value="10" />10</label></td>
<td><label><input name="RadioGroup7" type="radio" value="0" checked="checked" />0</label>
<br /><label><input type="radio" name="RadioGroup7" value="1" />1</label>
<br /><label><input type="radio" name="RadioGroup7" value="2" />2</label>
<br /><label><input type="radio" name="RadioGroup7" value="3" />3</label>
<br /><label><input type="radio" name="RadioGroup7" value="4" />4</label>
<br /><label><input type="radio" name="RadioGroup7" value="5" />5</label>
<br /><label><input type="radio" name="RadioGroup7" value="6" />6</label>
<br /><label><input type="radio" name="RadioGroup7" value="7" />7</label>
<br /><label><input type="radio" name="RadioGroup7" value="8" />8</label>
<br /><label><input type="radio" name="RadioGroup7" value="9" />9</label>
<br /><label><input type="radio" name="RadioGroup7" value="10" />10</label></td>
<td><label><input name="RadioGroup8" type="radio" value="0" checked="checked" />0</label>
<br /><label><input type="radio" name="RadioGroup8" value="1" />1</label>
<br /><label><input type="radio" name="RadioGroup8" value="2" />2</label>
<br /><label><input type="radio" name="RadioGroup8" value="3" />3</label>
<br /><label><input type="radio" name="RadioGroup8" value="4" />4</label>
<br /><label><input type="radio" name="RadioGroup8" value="5" />5</label>
<br /><label><input type="radio" name="RadioGroup8" value="6" />6</label>
<br /><label><input type="radio" name="RadioGroup8" value="7" />7</label>
<br /><label><input type="radio" name="RadioGroup8" value="8" />8</label>
<br /><label><input type="radio" name="RadioGroup8" value="9" />9</label>
<br /><label><input type="radio" name="RadioGroup8" value="10" />10</label></td>
<p align="center">
<label><input name="Submit" type="submit" id="Submit" value="Submit" /></label>
<label><input name="textfield1" type="text" id="textfield1" /></label>

Someone please help me. :D Thanks in advance.

07-06-2008, 01:20 PM
There are bound to be other considerations depending upon just what the ultimate use of this is. However, in about the simplest of terms (additions highlighted):

<title>Mah buttons. They do nothing.</title>
<script type="text/javascript">
function average(f){
for (var t = 0, r = f.elements, i = r.length - 1; i > -1; --i)
if(r[i].type && r[i].type == 'radio' && r[i].checked)
t += (r[i].value - 0);
r['textfield1'].value = t/8;
<form id="form1" name="form1" method="post" action="#" onsubmit="average(this);return false;">
<table width="100%" border="1">
<td><label><input name="RadioGroup1" type="radio" value="0" checked="checked" />0</label>
<br /><label><input type="radio" name="RadioGroup1" value="1" />1</label>
<br /><label><input type="radio" name="RadioGroup1" value="2" />2</label>
<br /><label><input type="radio" name="RadioGroup1" value="3" />3</label>
<br /><label><input type="radio" name="RadioGroup1" value="4" />4</label>
<br /><label><input type="radio" name="Radi . . .

07-06-2008, 02:21 PM
You are my sunshine.

My only sunshine.