Connecting Tech Pros Worldwide Forums | Help | Site Map

Internet Explorer and radio problem

wl
Guest
 
Posts: n/a
#1: Jul 23 '05
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




wl
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Internet Explorer and radio problem


"wl" <sorry@nospam.please> wrote in message
news:4288fb2b$0$13320$ba620e4c@news.skynet.be...[color=blue]
> 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).[/color]


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


RichB
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Internet Explorer and radio problem


Radio button group elements are in an array, so if you want to get the
type of one of them I think you would have to supply an index:

alert(document.forms[0].elements['***'][0].type);

http://www.DevPlug.com --Connecting Developers
Posted from: http://www.devplug.com/ftopic30452.htm
RobB
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Internet Explorer and radio problem


RichB wrote:[color=blue]
> Radio button group elements are in an array [..][/color]

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[i].type);

RobG
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Internet Explorer and radio problem


wl wrote:[color=blue]
> "wl" <sorry@nospam.please> wrote in message
> news:4288fb2b$0$13320$ba620e4c@news.skynet.be...
>[color=green]
>>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).[/color]
>
>
>
> 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[/color]

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

--
Rob
RichB
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Internet Explorer and radio problem


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 if you refer to it by name as you noted in your example
[color=blue]
> RobBwrote:[/color]
document.forms[0].elements.*** (an array, length == 3)[color=blue]
>[/color]

That is the array I was referring to, and as you noted it does not
have a "type" property.

http://www.DevPlug.com --Connecting Developers
Posted from: http://www.devplug.com/ftopic30452.htm
RobB
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Internet Explorer and radio problem


RichB wrote:[color=blue]
> 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 [..][/color]

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.
[color=blue]
> That is the array I was referring to, and as you noted it does not
> have a "type" property.[/color]

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

RichB
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Internet Explorer and radio problem


Yes, well perhaps my phrasing was poor, but it's still an array that
allows you to access the radio group elements that are grouped
according to name, and the reason the original attempt to retrieve
the type property fails is still because it is being referred to as
an array in the original example rather than as individual elements.

http://www.DevPlug.com --Connecting Developers
Posted from: http://www.devplug.com/ftopic30452.htm
Uday Bhasker Teerdhala
Guest
 
Posts: n/a
#9: Jul 23 '05

re: Internet Explorer and radio problem


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

wl wrote:[color=blue]
> 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[/color]
"radio" but[color=blue]
> "undefined" instead.
> For all other kinds ("text", "textarea", ... etc) of input fields the[/color]
type[color=blue]
> 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[/color]

Closed Thread