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

How can I read the value of a radio button?

P: n/a

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.
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk
Jul 20 '05 #2

P: n/a

"Fabian" <la****@hotmail.com> wrote in message
news:bq*************@ID-174912.news.uni-berlin.de...
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.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk


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

Jul 20 '05 #3

P: n/a

"Randell D." <re**********************@and.share.com> wrote in message
news:e9Qxb.518322$9l5.423971@pd7tw2no...

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=+%22ra...525ae%240%2411
375%24cc9e4d1f%40news.dial.pipex.com&rnum=3
Jul 20 '05 #4

P: n/a
Randell D. hu kiteb:
"Fabian" <la****@hotmail.com> wrote in message
news:bq*************@ID-174912.news.uni-berlin.de...
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?

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...

--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.