473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Internet Explorer and radio problem

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

Jul 23 '05 #1
8 2387
wl
"wl" <so***@nospam.please> wrote in message
news:42***********************@news.skynet.be...
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
Jul 23 '05 #2
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
Jul 23 '05 #3
RichB wrote:
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[i].type);

Jul 23 '05 #4
wl wrote:
"wl" <so***@nospam.please> wrote in message
news:42***********************@news.skynet.be...
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).

--
Rob
Jul 23 '05 #5
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
RobBwrote: document.forms[0].elements.*** (an array, length == 3)


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
Jul 23 '05 #6
RichB wrote:
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>

Jul 23 '05 #7
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
Jul 23 '05 #8
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:
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


Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Raghuram Banda | last post by:
Hi All, The following is the function I used to create RADIO buttons using DOM. It works fine with Netscape but not with IE. function addGroup3Radio() { var cellId =...
6
by: harry | last post by:
Hi, I have a program that runs on multiple client pc's. Occasionally one or more of those pc's use VPN to connect to another corporate network. When using VPN they need to set proxy server in...
4
by: Nicolás Castagnet | last post by:
Hi, I write this post because I notice a strange behavior related with "Temporary Internet Files" and maybe some of you can help me to understand it. I am working in a web application with...
6
by: pradeep_TP | last post by:
I am facing a strange problem with my web site. Afer reinstalling the web application on the web server, I am getting a dialog box for each page. The dialog box has the following information. ...
2
by: CathieC | last post by:
I have a websote developed using visual studio 2005 beta , .net version 2 i deploy my application to a server and it is run from client computers. One of the users gets the error "Internet...
11
by: Doug van Vianen | last post by:
Hi, I often like to include some JavaScript coding in my web pages to make them more interesting. Unfortunately, even when this coding is as simple as a check to see what the display width is in...
1
by: kebabkongen | last post by:
Hi, I'm working on a JavaScript that is enabling / disabling a select element according to whether a checkbox is selected or not. This works fine in Firefox, but in Internet Explorer (v 6.0.2900)...
2
alexphd
by: alexphd | last post by:
This code works in firefox perfectly, but in Internet Explorer it does not calculate the total correctly. Anybody know why? <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"...
2
by: swethak | last post by:
Hi, I am getting the problem the problem with google map in Internet Explorer. This map worked fine in mozilla . When i opened the same map in Internet Explorer i am getting the error...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.