document.getElementById() In Browsers

For non-form elements, like <div>, etc., document.getElementById() usually works stably, except Opera:

<div name="userName">1</div>
<div id="userName">2</div>
<script type="text/javascript">

Opera also checks name attribute, so the alerted message is “1”.

For form elements, IE and Opera have the same behavior:

<input type="text" name="userName" value="1" />
<input type="text" id="userName" value="2" />
<script type="text/javascript">

Here IE and Opera alerts “1”.


  • IE 6: Also checks name for form elements;
  • IE 7: The same as IE 6;
  • Firefox Normal;
  • Opera 9.24: Also checks name for any elements;
  • Safari 3.0.3: Normal.

For complex web pages, it would be good if there is a spec for elements naming.

1 Comment

Comments have been disabled for this content.