...

View Full Version : How can I store the name of a radio Button



mtkirwan
07-16-2004, 06:42 AM
Hi,

How can I store the name of a radio button?

The reason I am asking is that if I have determined that a radio button pair with name "myRadio1" has had the radio button with value "Yes" clicked I want to store the name of that particular radio button pair in an array.

Kind Regards,

Matthew

Kor
07-16-2004, 09:46 AM
You may send it to a function using this parameter and even build an array with all the name of radio buttons which were clicked 'YES':



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
var rad = new Array()
function storeN(n){
rad.push(n)
}
</script>
</head>

<body>
<form>
<input name="rad1" type="radio" value="yes" onclick="storeN(this.name)">
<input name="rad1" type="radio" value="no"><br>
<br>
<input name="rad2" type="radio" value="yes" onclick="storeN(this.name)">
<input name="rad2" type="radio" value="no">
</form>
</body>
</html>

Vincent Puglia
07-16-2004, 01:47 PM
Hi,
The following is built on Kor's suggestion, but it also accounts for checking the 'no' button by removing the 'yes' value in the array.



<html>
<head>
<script type="text/javascript" language="javascript">
var rad = new Array()

function storeN(oRad)
{
var val = oRad.name + "=" + oRad.value
if(oRad.value=='yes')
rad.push(val)
else
rad.pop();
}
</script>
</head>
<body>
<form>
<input name="rad1" type="radio" value="yes" onclick="storeN(this)">
<input name="rad1" type="radio" value="no" onclick="storeN(this)"><br>
<br>
<input name="rad2" type="radio" value="yes" onclick="storeN(this)">
<input name="rad2" type="radio" value="no" onclick="storeN(this)">
</form>
</body>
</html>


The following version is for 2 or more buttons; however, it isn't as 'perfect' as it could be -- removal of an array item leaves a null, so when checking values you need to look only at non-null values



<html>
<head>
<script type="text/javascript" language="javascript">
var rad = new Array()

function storeN(oRad)
{
var val = oRad.name + "=" + oRad.value
if(oRad.checked && oRad.value=='yes')
rad.push(val)
else
{
radName = oRad.name;
for (var i = 0; i < rad.length; i++)
if (rad[i].indexOf(radName) != -1)
rad[i] = null
}
alert(rad)
}
</script>
</head>
<body>
<form>
<input name="rad1" type="radio" value="yes" onclick="storeN(this)">
<input name="rad1" type="radio" value="no" onclick="storeN(this)"><br>
<br>
<input name="rad2" type="radio" value="yes" onclick="storeN(this)">
<input name="rad2" type="radio" value="no" onclick="storeN(this)">
</form>
</body>
</html>


Vinny

Kor
07-16-2004, 04:18 PM
Thanks, Vincent, you were well completed my initial ideea. :thumbsup:

Vincent Puglia
07-16-2004, 04:57 PM
Thanks, Vincent, you were well completed my initial ideea.

You're welcome ;)

Vinny

jamescover
07-17-2004, 03:33 AM
All of these script generate runtime errors in IE 5.0.... :confused:

Vincent Puglia
07-17-2004, 04:30 AM
Hi james,


... generate runtime errors in IE 5.0

Since I no longer have 5.0, I cannot really state anything with conviction, but it may be the 'push/pop'. To see if it is, replace the function with:

if(oRad.checked && oRad.value=='yes')
rad[rad.length] = val;
else
{
radName = oRad.name;
for (var i = 0; i < rad.length; i++)
if (rad[i].indexOf(radName) != -1)
rad[i] = null
}


Vinny
rad[rad.length] =

jamescover
07-18-2004, 09:37 AM
Hi Vinny:

Thanks for the code...but I was still getting a js error...

I had to reformat my HD and reinstall Windows today, so I will have to do some more testing and get back to you, once I recover old files....


Thanks, again...


-james

Vincent Puglia
07-18-2004, 04:40 PM
reformat my HD and reinstall Windows

:eek: ugh!

Vinny



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum