Internet Explorer and radio problem

W

wl

Hi,

I have a radiobutton group in HTML in a form:

Sex: <input type="radio" name="Sex" value="male">Male

<input type="radio" name="Sex" value="female">Female

<input type="radio" name="Sex" value="unknown">Unknown<br>



When I call document.forms[0].elements['Sex'].type I don't get "radio" but
"undefined" instead.
For all other kinds ("text", "textarea", ... etc) of input fields the type
is returning a correct
string.

It only seems to happen with Internet Explorer (using version 6).

What am I doing wrong ?

Thanks in advance,

Wim
 
W

wl

wl said:
Hi,

I have a radiobutton group in HTML in a form:

Sex: <input type="radio" name="Sex" value="male">Male

<input type="radio" name="Sex" value="female">Female

<input type="radio" name="Sex" value="unknown">Unknown<br>



When I call document.forms[0].elements['Sex'].type I don't get "radio" but
"undefined" instead.
For all other kinds ("text", "textarea", ... etc) of input fields the type
is returning a correct
string.

It only seems to happen with Internet Explorer (using version 6).


After hours of search I think I found the solution myself.

In case of a radiogroup each item is a different "element" and you should
search based on index instead of name:

eg: instead of using document.forms[0].elements['Sex'] use:
document.forms[0].elements[0] or document.forms[0].elements[1] or
document.forms[0].elements[2]
(when the radiobuttons are the first three components in the form of course)
These are the different radiogroup items (individually) and .type on these
objects return "radio" in fact

Wim
 
R

RobB

RichB said:
Radio button group elements are in an array [..]

Not really, unless you mean the Form.elements array (really a DOM
collection). The objects themselves are collected without any
particular grouping; the grouping is done at:

Form.element_name

....and

Form.elements.element_name

So for

<input type="radio" name="Sex" value="male">Male
<input type="radio" name="Sex" value="female">Female
<input type="radio" name="Sex" value="unknown">Unknown

....you'll find references to the three Radio objects at

document.forms[0].elements.Sex (an array, length == 3)

....or simply:

document.forms[0].Sex

...although the shorthand form is not guaranteed to work. This array,
naturally, has no 'type' property; you need to extract its elements
separately, or, more commonly, in a loop:

var r_arr = document.forms[0].elements.Se­x;
for (var i = 0, l = r_arr.length; i < l; ++i)
alert(r_arr.type);
 
R

RobG

wl said:
Hi,

I have a radiobutton group in HTML in a form:

Sex: <input type="radio" name="Sex" value="male">Male

<input type="radio" name="Sex" value="female">Female

<input type="radio" name="Sex" value="unknown">Unknown<br>



When I call document.forms[0].elements['Sex'].type I don't get "radio" but
"undefined" instead.
For all other kinds ("text", "textarea", ... etc) of input fields the type
is returning a correct
string.

It only seems to happen with Internet Explorer (using version 6).



After hours of search I think I found the solution myself.

In case of a radiogroup each item is a different "element" and you should
search based on index instead of name:

eg: instead of using document.forms[0].elements['Sex'] use:
document.forms[0].elements[0] or document.forms[0].elements[1] or
document.forms[0].elements[2]
(when the radiobuttons are the first three components in the form of course)
These are the different radiogroup items (individually) and .type on these
objects return "radio" in fact

As an aside, you should also make one of your radio buttons checked by
default (the 'unknown' one?). The spec says that one should always be
selected, most browsers will make the first one checked if you don't
make a choice but that should not be relied on (and you probably
shouldn't presume your audience is male by default, but that's your
choice).
 
R

RobB

RichB said:
Yes, I understand that you can access the individual radio elements
through forms[0].elements[0], but you can also access the group like
an array [..]

That's because you ^are^ accessing an array - it's just not an array
where the radio (input) objects are grouped, but where references to
them are grouped. The actual objects are found where form element
objects usually are, in the elements[] collection. They're given no
special treatment afaik because they're radios, or same-named.
That is the array I was referring to, and as you noted it does not
have a "type" property.

<quote>
Radio button group elements are in an array...
</quote>
 
U

Uday Bhasker Teerdhala

Hi Wim,
I am udaybhasker Teerdhala,

For ur problem do following.
If u get 'undefined' there check for this validation.
In your example check for the following condition and proceed.

Code:

if(typeof(document.forms[0].elements['Sex'])!=typeof(undefined))
{
alert(document.forms[0].elements['Sex']);
}
Then definitely ur problem will be solved out...
 

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
474,002
Messages
2,570,261
Members
46,859
Latest member
VallieMcKe

Latest Threads

Top