Wednesday, April 18, 2007

IE getElementById Bug

If there are two elements in a page with the same name, say for example a hidden field and a meta tag, Internet Explorer won't let you get one of them by ID. Why? Because IE is stupid.

The solution: for form elements at least, you can fall back on old school antics such as document.FORMNAME.elements["fieldName"], but it would be advisable to wrap that in a function so that somebody on your team (or yourself a month later when you've forgotten the problem) doesn't come along and replace the old code with a $() call.

Code sample: TODO. (if a simple test case fails to reproduce the problem I'll delete this post, but I've encountered the issue twice now, so I think it will)

1 comment:

Anonymous said...

Yeah, this is very reproducable whenever an element with a name attribute appears before the element you want, but the name matches the id you are looking for.

But GOOD NEWS! there is a fix for this! Buddy over at this site:

http://webbugtrack.blogspot.com/2007/08/bug-152-getelementbyid-returns.html

has found a way to fix IEs version of the method to return the right one every time!