...

View Full Version : Unable to get radiobutton values



trsands
01-06-2004, 01:06 PM
This script gives always gives an oujtput of 'undefined' got the radiobutton values.. can anyone see why?



<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
function showgame()
{
var frm = document.forms['form1'];
frm.textfield.value = frm.RG1.value;

}

</script>
</head>

<body>
<p>&nbsp; </p>
<form name="form1" method="post" action="">
<p>&nbsp;</p>
<p>
<label></label>
</p>
<p>
<label>
<input type="radio" name="RG1" value="D">
DTC</label>
Input
<input type="text" name="textfield">
<br>
<label>
<input type="radio" name="RG1" value="M">
MIA</label>
<br>
<label>
<input type="radio" name="RG1" value="L">
LNC</label>
</p>
<p>
<input type="Button" name="Button" value="Button" onClick="showgame()">
</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</form>
<p>
<label></label>
</p>
<p>
<label></label>
<br>
</p>
<p>&nbsp; </p>
</body>
</html>

glenngv
01-06-2004, 01:23 PM
html controls with same name becomes an array. So you access it like frm.RG1[0].value, frm.RG1[1].value, and so on. But what you want is the value of the checked button because values of unchecked radios are not changed.


function showgame()
{
var frm = document.forms['form1'];
for (var i=0;i<frm.RG1.length;i++){
if (frm.RG1[ i ].checked) {
frm.textfield.value = frm.RG1[ i ].value;
break;
}
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum