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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to calculate average using radio buttons!

Status
Not open for further replies.

clubing4esx

Technical User
Jan 23, 2007
3
GB
hi guys this is my prob..i keep getting back NaN...not sure why heres my code....any suggestions be helpful!

<script language="JavaScript" type="text/javascript">
<!--

cnt=1;
mags=0;

function doAve(form){
mags=mags+parseFloat(form.mags.value);
var average=eval(mags/cnt);
form.average.value=average;
cnt++;
}
//-->
</script>


and the body...


<form>
<table border ="1">
<tr>
<td colspan="5" align="center">
<img name="Mags" border="0" src="C:\Documents and Settings\Richard\Desktop\pics\1.jpg" width="145" alt="Mags" OnMouseover="window.status='Richards First!'; return true" OnmouseOut="window.status='Rate My Conquests'; return true"</td>
</td>
</tr>
<tr>
<td align="center"><input name="mags" Value="1" type="radio" onclick="doAve(this.form)"></td>
<td align="center"><input name="mags" Value="2" type="radio" onclick="doAve(this.form)"></td>
<td align="center"><input name="mags" Value="3" type="radio" onclick="doAve(this.form)"></td>
<td align="center"><input name="mags" Value="4" type="radio" onclick="doAve(this.form)"></td>
<td align="center"><input name="mags" Value="5" type="radio" onclick="doAve(this.form)"></td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">2</td>
<td align="center">3</td>
<td align="center">4</td>
<td align="center">5</td>
</tr>
<tr>
<td colspan="5" align="center">Average <input type="text" value="0" name="average" size="20">
</td>

any suggestions be reallly appreciated!! cheers!
 
your problem is that you have an <img> tag with the name mags as well, names are not case sensitive and it grabs the undefined value from the <img> tag
 
Hi thanks monksnake it really really appreciated. Have ammended code accordingly..however it still says the same? thanks for your help anyways Monksnake its hugely welcome!
 
Try this instead:
Code:
function doAve(form)
{
var magvalue;
var magradio = form.elements['mags'];

alert(mags);

for (var mi=0;mi<magradio.length;mi++)
  {
  if (magradio[mi].checked)
    {
    mags=mags+parseFloat(magradio[mi].value);
    break;
    }
  }


var average=mags/cnt;
form.average.value=average;
cnt++;
}

You need to access the radio button array as an array and see which button is checked.

Lee
 
Thank you Lee (trollacious) very much appreciated and the code works wonderfully! Thanks again you people are amazing!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top