big IE bug.. or am I crazy?

Code Snippet

<html>
<head>
<title>IE BUG!!!!!!</title>
<script language='JavaScript'>
function test(){
alert(window.document.getElementById('el666').getAttribute('value'));
}
</script>
</head>
<body>

<input type="text" id="el888" name="el666" value="input 1" />

<input type="text" id="el666" name="el777" value="input 2" />

<input type="button" value="GO" onclick="JavaScript:test();" />

</body>
</html>

the result of the "alert" shoud be "input 2", but IE shows "input 1" ?!?!?

How can this be?

Firefox run it well!

When I use the method "getElementById", I want to get the element that as that ID.. not that NAME!

Can anyone explain this to me? Am I crazy or what?

[1133 byte] By [RuiM] at [2008-1-10]
# 1
Always use the same value for "id" and "name" as a good coding practice. This may be a bug but my bet is that you can't come up with a valid scenario where it's absolutely necessary to have different values.
skeeter2 at 2007-10-3 > top of Msdn Tech,Internet Explorer Development,Internet Explorer Web Development...
# 2
Yes, confirmed.

This is a well known, long standing bug (since IE implemented the ECMAScript .getElementById( ID ) method.

We have (all developers) been waiting for the IE Team to speak up on how/when they plan to fix this so that it follows not only the spec, but the actual name of the method itself.

As for the other reply in this thread, the problem is that other non-input elements can have a name, and thus you can get collisions.

If any of your anchor tags, meta tags, object tags, frame/iframe tags have a matching name value, THEY WILL BE RETURNED FIRST!... which is a MAJOR BUG!

Stay tuned to the IE Blog for more info.

unique_username at 2007-10-3 > top of Msdn Tech,Internet Explorer Development,Internet Explorer Web Development...
# 3
ok. thank you.

For a moment.. I thought I was getting crazy.. hehehe

I hope the MS dev team for IE fix this MAJOR BUG soon..

RuiM at 2007-10-3 > top of Msdn Tech,Internet Explorer Development,Internet Explorer Web Development...
# 4
RuiM;

You can get a great workaround for this bug from this site;

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

They redefine the broken IE version of getElementById with one that works correctly.

HTH!

Bobby

Bobby20072 at 2007-10-3 > top of Msdn Tech,Internet Explorer Development,Internet Explorer Web Development...
# 5

Thank you for your reply.

it's a valid workaround, but in complex and big pages with many elements, it can be a little slow.

I hope that MS correct this in a near future. Is it realy so hard to fix this? I think not!

Rui

RuiM at 2007-10-3 > top of Msdn Tech,Internet Explorer Development,Internet Explorer Web Development...