How can I read the value of a radio button?

R

Randell D.

Folks,

I have a form (called FORM1) - In my INPUT submit tag, I have an onClick
event that I have successfully tested/used to display the value of a TEXT
box using the following function (called via an onClick event)

<script type="text/javascript">
function performPrePostChecks()
{
// myName being an iput text box
// hosted being an input radio box
h=document.form1.myName.value;
alert(h);
return false;
}

How can I read the value of a radio button like I have the text box?

At the moment, if I have it catch myName, I get the value I entered in to
the box named myName in an alert box.

When I change the function to instead catch the value of the radio button
(called hosted) - The alert box says "undefined".

Are radio buttons handled differently?

I know the above function is basic - I've just narrowed my problem down to
not reading the radio button properly hence no need for me to list the rest
of the code.

All help, via the newsgroup would be much appreciated,
Thanks
Randell D.
 
F

Fabian

Randell D. hu kiteb:
Folks,

I have a form (called FORM1) - In my INPUT submit tag, I have an
onClick event that I have successfully tested/used to display the
value of a TEXT box using the following function (called via an
onClick event)

<script type="text/javascript">
function performPrePostChecks()
{
// myName being an iput text box
// hosted being an input radio box
h=document.form1.myName.value;
alert(h);
return false;
}

How can I read the value of a radio button like I have the text box?

At the moment, if I have it catch myName, I get the value I entered
in to the box named myName in an alert box.

When I change the function to instead catch the value of the radio
button (called hosted) - The alert box says "undefined".

Are radio buttons handled differently?

Radio buttons require an explicit VALUE attribute. CHECKED is an
optional attribute and can be used to specify which options are selected
for initial form display. The VALUE attribute for a text box is whatever
you type there, but typing is unlikely in the context of a radio button.
 
R

Randell D.

Fabian said:
Randell D. hu kiteb:


Radio buttons require an explicit VALUE attribute. CHECKED is an
optional attribute and can be used to specify which options are selected
for initial form display. The VALUE attribute for a text box is whatever
you type there, but typing is unlikely in the context of a radio button.


--

Okay... I'm familiar with the HTML side of my form and how to set an initial
form display - but how do I use javascript to read my radio value that a
user has selected/checked from radio buttons. My form is called FORM1 and
the following code is what I use to display the radio buttons in the first
place:

Hosted:
<input type="radio" name="hosted" value="local"> Locally
<input type="radio" name="hosted" value="remote"> Remote

randelld
 
R

Randell D.

Randell D. said:
Folks,

I have a form (called FORM1) - In my INPUT submit tag, I have an onClick
event that I have successfully tested/used to display the value of a TEXT
box using the following function (called via an onClick event)

<script type="text/javascript">
function performPrePostChecks()
{
// myName being an iput text box
// hosted being an input radio box
h=document.form1.myName.value;
alert(h);
return false;
}

I hear so much about "Google being your friend" that I checked their archive
of the newsgroup and found the following answer that I was able to
incorporate into my own solution... My thanks to a chap called "Richard
Hockley" who posted it a few months ago...

http://www.google.ca/groups?q=+"rad...ie=UTF-8&oe=UTF-8&as_qdr=y&selm=3f4525ae$0$11
375%24cc9e4d1f%40news.dial.pipex.com&rnum=3
 
F

Fabian

Randell D. hu kiteb:
Hosted:
<input type="radio" name="hosted" value="local"> Locally
<input type="radio" name="hosted" value="remote"> Remote

<script language="javascript">
function foo(loo) {
var i = loo.value;
alert(i);
}
</script>
</head>
<body>


<form name="plar">
<input type="radio" name="yeeq" value="local" onclick="foo(this);">
Locally
<input type="radio" name="yeeq" value="remote" onclick="foo(this);">
Remote
</form>

Try what I wrote above. istm that radio buttons have funny stuff going
on because they are defined by multiple input tags. If I had designed
those tgs, I would have made it in the same way the select and option
tag works...
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top