If you think a bug might affect users in the 57 release, please set the correct tracking and status flags for Release Management.

JavaScript getElementById does not create array for Radio buttons with same name

VERIFIED WONTFIX

Status

()

Core
DOM
P3
normal
VERIFIED WONTFIX
18 years ago
17 years ago

People

(Reporter: Tim Powell, Assigned: jst)

Tracking

Trunk
x86
Windows NT
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

18 years ago
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (Windows; U; WinNT4.0; en-US; m16) Gecko/20000606
BuildID:    2000060608

Trying to use document.getElementById as a replacement for IE's document.all. I
want to reference a radio button set and check its length and determine which
one is checked. getElementByID only gives a reference to a particular element
and not the collection.

Reproducible: Always

Here's simplified sample code that demonstrates the problem.

<form name="frm">
<label>
<input type="Radio" name="rad" value="0" checked="checked"> Button 0
</label><br>
<label>
<input type="Radio" name="rad" value="1"> Button 1
</label><br>
</form>

<script language="JavaScript">
alert("Radio array length using explicit reference: " + document.frm.rad.length
+ " (should be 2)");
alert("Radio array length from getElementById: " + document.getElementById
('rad').length + " (should be 2)");
// The following comment is what you'd use for IE with document.all
// alert("Radio array length from document.all: " + document.all['rad'].length +
" (should be 2)");
</script>

This is somewhat like the problem described in bug 18840 except instead of
referencing by document.form[0] explicitly I'm using document.getElementById.

The DOM 1 HTML spec says that behavior is undefined when multiple elements have
the same ID (in this case form elements with the same name). It would seem
logical to collect them together into an array. getElementById works like
Mozilla in IE5, but document.all creates the array of radio elements. It'd be
nice if Mozilla could use getElementById as a replacement for document.all. (Or
implement document.all)
(Assignee)

Comment 1

18 years ago
The DOM clearly defines that getElementById returns an element whose *ID*
matches the argument given to getElementById, and the method name also implies
that the method does indeed return only one *element* (and so does the return
type). ID's are document unique identifiers and thus you can not have more than
one element with the same ID, NAME attributes are not ID's and getElementById
should not even find elements whose NAME happens to match the given argument (in
mozilla it does, but IMO it shouldn't).

It sounds like you should be using the getElementsByName() method and not
getElementById().

I'm marking this as WONTFIX since we'd be breaking the DOM spec and there's no
backwards compatibility to consider here...
Status: UNCONFIRMED → RESOLVED
Last Resolved: 18 years ago
Resolution: --- → WONTFIX

Comment 2

18 years ago
No failure found in getElementById(). IDs must be unique in a document according 
to HTML 4.0 spec at: http://www.w3.org/TR/REC-html40/struct/global.html#adef-id
Marking Verified.
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.