By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,963 Members | 1,749 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,963 IT Pros & Developers. It's quick & easy.

unable to do an alert for a radio button value

P: n/a
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!

Oct 19 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"yawnmoth" <te*******@yahoo.comwrote in message
news:11**********************@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>


Oct 19 '06 #2

P: n/a

yawnmoth wrote:
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?

Oct 19 '06 #3

P: n/a

nutso fasst wrote:
"yawnmoth" <te*******@yahoo.comwrote in message
news:11**********************@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!

Oct 19 '06 #4

P: n/a

"yawnmoth" <te*******@yahoo.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
>
nutso fasst wrote:
"yawnmoth" <te*******@yahoo.comwrote in message
news:11**********************@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

Oct 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.