|
 |
asp_web_howto thread: Reading Radio button values in JavaScript
Message #1 by "Chris Chaffee" <chris.chaffee@u...> on Wed, 25 Apr 2001 11:26:23 -0700
|
|
Hello,
I'm having some difficulty finding the correct way to read the value of
a radio button group in JavaScript. I have a form with three Radio
Input fields, all with the same name. Normally, I would just do
something like 'document.form.itemname.value' to get the value, but when
there are multiple objects with the same name I just get 'undefined'.
How do I read the value of the selected radio button?
Thanks,
Chris Chaffee
Technology Engineer
UNX, Inc.
Member NASD/SIPC
175 E. Olive Ave., 2nd Fl.
Burbank, CA 91502
(xxx) xxx-xxxx x317
chris@u...
Message #2 by Michael Filip <mzcfilip@y...> on Wed, 25 Apr 2001 12:13:39 -0700 (PDT)
|
|
lets say you have 3 radio buttons like so:
<input type="radio" name="Sex" value="male"><BR>
<input type="radio" name="Sex" value="female"><BR>
<input type="radio" name="Sex" value="androgenous">
and you want to check if one is checked, for
validation purposes.
Here's a sample "if" statement in Javascript that I
use... now
there may be better ways, like a "case" equivalent,
but I know
it not.
// Snippet
if(document.form.sex[0].checked==false){
if(document.form.sex[1].checked==false){
if(document.form.sex[2].checked==false){
alert("Didn't I tell you to indicate your sex? Go
do it!");
document.form.sex[0].focus();
return false;
}
}
}
// End Snippet
Now, the radio buttons are seen as an Array, beginning
at 0.
The property you're looking for is "checked", and is
boolean.
I put in the ".focus()" line cuze I've found it to be
a neat
way of indicating to the user where their problem is.
To find the value of a clicked button, I usually use
the following (and hope there aren't lots of buttons):
if (document.form.Sex[0].checked==true){
alert("Sex value is |" + form.Sex[0].value + "|");
}
So you see you use the same ".value" property, you
just have to know what button in the array was
checked. Actually, you might be able to get it
another way, but I can't recall what.
Now that you have that, if you need anything else try
this link:
http://www.vsv.slu.se/johnb/java/jslref.htm
It's a great javascript reference.
cheers,
mike
--- Chris Chaffee <chris.chaffee@u...> wrote:
> Hello,
>
> I'm having some difficulty finding the correct way
> to read the value of
> a radio button group in JavaScript. I have a form
> with three Radio
> Input fields, all with the same name. Normally, I
> would just do
> something like 'document.form.itemname.value' to get
> the value, but when
> there are multiple objects with the same name I just
> get 'undefined'.
> How do I read the value of the selected radio
> button?
>
> Thanks,
>
> Chris Chaffee
> Technology Engineer
> UNX, Inc.
Message #3 by "Anil Rhemtulla" <AnilR@T...> on Thu, 26 Apr 2001 18:50:30 +0200
|
|
For javascript:
You need to loop through all the radio buttons (with that name) and see
which on has the "checked" property set.
For ASP:
ValueofSelectedRadio = Request.Form("NameOfRadioButtons")
Cheers,
Anil
----- Original Message -----
From: "Chris Chaffee" <chris.chaffee@u...>
To: "ASP Web HowTo" <asp_web_howto@p...>
Sent: Wednesday, April 25, 2001 8:26 PM
Subject: [asp_web_howto] Reading Radio button values in JavaScript
> Hello,
>
> I'm having some difficulty finding the correct way to read the value of
> a radio button group in JavaScript. I have a form with three Radio
> Input fields, all with the same name. Normally, I would just do
> something like 'document.form.itemname.value' to get the value, but when
> there are multiple objects with the same name I just get 'undefined'.
> How do I read the value of the selected radio button?
>
> Thanks,
>
> Chris Chaffee
> Technology Engineer
> UNX, Inc.
> Member NASD/SIPC
> 175 E. Olive Ave., 2nd Fl.
> Burbank, CA 91502
> (xxx) xxx-xxxx x317
> chris@u...
>
>
|
|
 |