unable to do an alert for a radio button value

Y

yawnmoth

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!
 
N

nutso fasst

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

yawnmoth

yawnmoth said:
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?
 
Y

yawnmoth

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

Hmmm - I guess we posted at the same time. Anyway, that seems to do it
- thanks!
 
N

nutso fasst

yawnmoth said:
nutso said:
<a href="#" onclick="for(var i=0; !document.forms[0].testvar; i++){};
alert(document.forms[0].testvar.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.checked;
i++){};
alert(document.forms[0].testvar.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.checked && i
< document.forms[0].testvar.length; i++){};
alert(document.forms[0].testvar.value); return false">click me</a>


SORRY!

nf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top