Posts tagged ie

IE getElementById Bug Strikes Again

0

I was burned by the document.getElementById() may return incorrect element in IE and Opera bug again today. This time though it was particularly bothersome as I spent the better part of the afternoon trying to figure out why I couldn’t access the length property of the value of a textarea element in IE when the same code worked fine in Firefox. I mean, getting the length of a value of any form element through JavaScript usually isn’t like rocket science.

<textarea id="description" name="description">Here is some text</textarea>

<script type="text/javascript">
var myElement = document.getElementById('description');
alert(myElement.value.length); //alert: 17
</script>

Every time I tried to execute that code in IE it would complain that 'value.length' is null or not an object.

After a whole crap load of debugging I finally tracked it down to the fact that IE was actually referencing the <meta name=”description”> tag in the head section of the page. Yeah, see even though the function is getElementById, IE still thought I meant the element with the name attribute equal to “description”. Or as my brother put it in a comment to the original post (before the comments were wiped in the great Data Loss of ’06), maybe IE thought I was calling the getJustAboutAnythingIFeelLike() function…

Damn you IE for making me have to change my ID attributes to suite your leisurely implementation of the DOM!

* This post was originally published on April 5, 2007 at http://www.csb7.com/blogs/whyblogwhy/2007/04/05/ie_getelementbyid_bug_strikes_again

document.getElementById() may return incorrect element in IE and Opera

0

I came across a bug in Internet Explorer whereby the document.getElementById() function may return an element with a name attribute equal to the id that is specified. This can cause unexpected results.

According to the W3C’s DOM Level 2 Core Specification:

getElementById introduced in DOM Level 2
Returns the Element whose ID is given by elementId. If no such element exists, returns null. Behavior is not defined if more than one element has this ID.

Note: The DOM implementation must have information that says which attributes are of type ID. Attributes with the name “ID” are not of type ID unless so defined. Implementations that do not know whether attributes are of type ID or not are expected to return null.

Parameters

elementId of type DOMString
The unique id value for an element.

Return Value

ElementThe matching element.

No Exceptions

http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-getElBId

However, for the browsers that failed the test, it appears that their implimentation of getElementById returns the first element where the ID or name attribute is equal to the specified ID.

This bug was tested against the following browsers:

  • IE 6.0 sp2: failed
  • IE 5.5: failed
  • IE 5.01: failed
  • Netscape 7.2: passed
  • Netscape 6: passed
  • Mozilla 1.7.3: passed
  • Opera 8.2: failed
  • Firefox 1.0.6: passed

For instance, the following code should produce an alert with “foo=foo; bar=bar”. However, the browsers that fail the test actually return “foo=foo; bar=foo”.

Test Page

Reported at http://www.quirksmode.org/bugreports/index.html 2005-08-29

Noted on http://channel9.msdn.com/wiki/default.aspx/Channel9.InternetExplorerProgrammingBugs

* This post was originally published on August 29, 2005 at http://www.csb7.com/whyblogwhy/index.php/2005/08/29/ie_getelementbyid_bug/

Go to Top