getElementById(‘description’) in IE

June 3rd, 2005. Tagged: JavaScript

document.getElementById() in IE has some issues. The problem can be spotted when you have a page element with an id "description" and meta tag with a name "description" IE considers the meta tag to be the one that should be returned by getElementById(). Which is, of course, wrong. ID is an ID and name is a totally different thing.

Anyway, heads up for that IE behaviour.

As an illustration, here in WordPress (the blogging tool that powers the page you're reading) there's a meta tag, called "generator", like this:
<meta name="generator" content="WordPress 1.5" />

So if you do:
this will return null in FireFox, the way it was meant to be (by w3c, that is) and in IE, you'll get [Object]

Try it...
Try it with the "content" property...
(The second example will give an error in FireFox as there is no "content" property of null.

Tell your friends about this post: Facebook, Twitter, Google+

7 Responses

  1. IE7 is still suffering from this bug… :(

  2. I just ran into this bug. I’m having two login forms on a page. The corresponding elements placed in different forms have the same names but different ids. Still when using getElementByID IE grabs the value of the first field it finds with that name. Totally wrong of course… But this is something we’re all used to when dealing with MS software. :P

    Did you happen to find a workaround for it or something?

  3. Thanks Janos for pointing that out. This is redicioulous, getElementById is not what it is :D Apparently IE gets a little confused what is a name and what is an id attribute.

    I did this little test as an illustration, this alert()s 1 in IE and 2 in Firefox.

    <form action="#">
    <input type="text" id="someid1" name="t2" value="1" />
    <input type="text" id="t2"      name="t2" value="2" />
    <a href="javascript:alert(document.getElementById('t2').value)">test</a>
  4. This site:

    Has the perfect solution to this bug. It redefines the method so that IE always returns the correct object without causing a huge performance hit.

    Check it out, it works great!

  5. Well it was about time I found this page. Me and my teammates just wasted 3 hours on our project because of that. You totally saved the day haha. Microsoft should really fix this.

    Thank you

  6. [...] has been a source of frustration before with IE, it’s really hard to understand the logic behind it, but it’s also happening in [...]

  7. Amazing! Its really awesome article, I have got much clear idea
    on the topic of from this piece of writing.

Leave a Reply