Skip to content

Obviate.io

To anticipate and prevent

  • Home
  • About Us
  • History
  • Privacy Policy
  • Toggle search form

document.getElementById not working in IE8

Posted on 2011-02-14 By John No Comments on document.getElementById not working in IE8

The title is a bit misleading. document.getElementById actually does work in IE8, it just works differently than it did in previous versions.

Previously (in IE8 Compatibility Mode and earlier), getElementById actually searched based on name and id. This means that lazy or ignorant developers might have gotten used to using name when they meant id. This would have worked fine for the last several years, but with IE8 came Standards!

The real crux of the problem is that when there are colons (:) in the id of the property (which is generally also the name), they get converted to underscores (_).

In an ideal world, the fix would be to use a method named getElementByName, but there is no such thing. Fortunately, you can easily convert the name to an id with a quick replace:

<br />
reg = /:/g;<br />
strId = strName.replace(reg, "_");

If you want to get fancy about it, you can even make your own function:

function convertNameToId(strId)<br />
{<br />
reg = /:/g;<br />
return strId.replace(reg, "_");<br />
}

Thus you could change a call of document.getElementById(strName) to document.getElementById(convertNameToId(strName)) and you would be good.

I’d like to thank blogger Max Graham for his comment on Yosif Yosifov’s post getElementById compared in IE6, IE7 and IE8, which helped me figure out why things were going wrong.

Code, Microsoft Tags:getElementById, IE, IE6, IE7, IE8, javascript

Post navigation

Previous Post: Wiping your data from the Augen Gentouch78
Next Post: Amazon MP3 and Cloud Storage

More Related Articles

Let WordPress (not) snow Site Maintenance
Video: Overview of MeteorJS development using AtomIO Video
SocketIO IRC-style Tutorial – Part 4 – The client code Cloud

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

February 2011
S M T W T F S
 12345
6789101112
13141516171819
20212223242526
2728  
« Jan   Mar »

amazon Android anime apache apple arduino asus averatec AWS Bad Company 2 cloud DIY eee fanime fanimecon github google iPad iphone IPv6 javascript kindle linkedin linux macbook air mass effect nodejs openvpn osx php review San Francisco security tutorial Ubuntu urban garden usb video wifi windows 7 winter urban garden wordpress xbox xbox 360 XboxLIVE

Copyright © 2022 Obviate.io

Powered by PressBook Premium theme