Connecting Tech Pros Worldwide Forums | Help | Site Map

unable to do an alert for a radio button value

yawnmoth
Guest
 
Posts: n/a
#1: Oct 19 '06
I'm trying to display a popup showing which radio button is selected
and am unable to do so. Every time I try, I get undefined, instead of
the value of the particular form variable I'm trying to get. Here's my
code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="">
<input type="radio" name="testvar" value="1"
checked="checked">option # 1<br>
<input type="radio" name="testvar" value="2">option # 2<br>
<input type="radio" name="testvar" value="3">option # 3<br>
</form>
<br>
<a href="#" onclick="alert(document.forms[0].testvar.value); return
false">click me</a>
</body>
</html>

If I just use the default selections, it seems like I should be getting
an alert that says "1", but I'm not. I'm getting an alert that says
"undefined" and I don't know why. Any ideas would be appreciated -
thanks!


nutso fasst
Guest
 
Posts: n/a
#2: Oct 19 '06

re: unable to do an alert for a radio button value



"yawnmoth" <terra1024@yahoo.comwrote in message
news:1161292256.453233.189670@e3g2000cwe.googlegro ups.com...

<a href="#" onclick="for(var i=0; !document.forms[0].testvar[i]; i++){};
alert(document.forms[0].testvar[i].value); return false">click me</a>




yawnmoth
Guest
 
Posts: n/a
#3: Oct 19 '06

re: unable to do an alert for a radio button value



yawnmoth wrote:
Quote:
I'm trying to display a popup showing which radio button is selected
and am unable to do so. Every time I try, I get undefined, instead of
the value of the particular form variable I'm trying to get. Here's my
code:
>
<snip>
>
If I just use the default selections, it seems like I should be getting
an alert that says "1", but I'm not. I'm getting an alert that says
"undefined" and I don't know why. Any ideas would be appreciated -
thanks!
This problem appears to be exclusive to radio buttons since the onclick
event in the anchor tag works just fine in this example (the only thing
that's changed is what testvar is):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="">
<input type="hidden" name="testvar" value="1">
</form>
<br>
<a href="#" onclick="alert(document.forms[0].testvar.value); return
false">click me</a>
</body>
</html>

Any ideas?

yawnmoth
Guest
 
Posts: n/a
#4: Oct 19 '06

re: unable to do an alert for a radio button value



nutso fasst wrote:
Quote:
"yawnmoth" <terra1024@yahoo.comwrote in message
news:1161292256.453233.189670@e3g2000cwe.googlegro ups.com...
>
<a href="#" onclick="for(var i=0; !document.forms[0].testvar[i]; i++){};
alert(document.forms[0].testvar[i].value); return false">click me</a>
Hmmm - I guess we posted at the same time. Anyway, that seems to do it
- thanks!

nutso fasst
Guest
 
Posts: n/a
#5: Oct 20 '06

re: unable to do an alert for a radio button value



"yawnmoth" <terra1024@yahoo.comwrote in message
news:1161295653.085091.145070@m7g2000cwm.googlegro ups.com...
Quote:
>
nutso fasst wrote:
Quote:
"yawnmoth" <terra1024@yahoo.comwrote in message
news:1161292256.453233.189670@e3g2000cwe.googlegro ups.com...

<a href="#" onclick="for(var i=0; !document.forms[0].testvar[i]; i++){};
alert(document.forms[0].testvar[i].value); return false">click me</a>
Hmmm - I guess we posted at the same time. Anyway, that seems to do it
Oops. Actually, it won't do it because I left out the checked property in
the test. I meant to type:

<a href="#" onclick="for(var i=0; !document.forms[0].testvar[i].checked;
i++){};
alert(document.forms[0].testvar[i].value); return false">click me</a>

But even that is a bit dangerous. Prolly should be:

<a href="#" onclick="for(var i=0; !document.forms[0].testvar[i].checked && i
< document.forms[0].testvar.length; i++){};
alert(document.forms[0].testvar[i].value); return false">click me</a>


SORRY!

nf



Closed Thread