document.getElementById() returns element with name equal to id specified

The document.getElementById() function returns an element with a name attribute that is equal to the id specified.

Test page Workaround is not included
Reported by: Chris Bloom.

Explorer 5-6 Windows, Explorer 7, Opera | Reported on 14 September 2005.

This site heavily relies on bug reports created by its readers. Anyone can report a bug and be published.




Search reports by browser:

Atom RSS

Comments

(Add your own)

1 Posted by Milo van der Leij on 14 September 2005 | Permalink

In their defense: "The id and name attributes share the same name space." (http://www.w3.org/TR/html4/struct/links.html#h-12.2.3)

But that still doesn't make it right.

2 Posted by Olly on 14 September 2005 | Permalink

I would guess that Opera is purposely mimicking IE's behaviour, for maximum compatibility.

3 Posted by Hallvord R. M. Steen on 31 October 2006 | Permalink

I can confirm that Opera's behaviour is by design. It un-broke some important page that had broken itself by expecting this implementation.

And yes, we know it's wrong :-(

4 Posted by Gérard Talbot on 5 September 2007 | Permalink

This has been fixed in Opera 9.50a1 build 9500.

Gérard

5 Posted by Geoff Corriere on 8 January 2008 | Permalink

OK. My bad. I did not get it right the first time. I posted a comment that used a do/while loop to return an element using getElementById and then looked at the actual id of the node to see if it was what I wanted. (That post may not appear on this page)

I ended up in an infinite loop.

This works better.

var element_id = 'my_id';
var element_node;

// get all elements with a given NAME attribute value
var nodes_arr = document.getElementsByName(element_id);
var limit = nodes_arr.length;
// loop the array of elements
for (var x=0; x<limit; x++) {
// check the ID attribute of the current array element
if (nodes_arr[x].id == element_id) {
element_node = nodes_arr[x];
x = limit; // end the loop
}
}

Post a comment

Commenting guidelines:

  1. When quoting specs, articles or your own research, please include a URL of the document.
  2. Test your stuff. When reporting browser behaviour, make sure that your report is correct.

Yes