Browser Bug: document.getElementById() gets the wrong element (IE)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    Browser Bug: document.getElementById() gets the wrong element (IE)

    Problem
    document.getEle mentById() selects an element by its name.

    Browser
    Internet Explorer

    Example
    Relevant HTML code:
    [HTML]<input name="test" type="text" value="test">[/HTML]
    The Javascript code:
    [CODE=javascript]var obj = document.getEle mentById("test" );[/CODE]
    This will not result in an error and the element is available as 'obj' even though no element with the id "test" exists on the page.

    Solution
    This is a bug which will cause problems in most, if not all, other browsers. document.getEle mentById() should get an element by its ID as the name suggests, not by its name. This will also cause problems in IE when you have an element with a name the same as the ID of the element you actually want to access. The wrong element will be accessed!

    Be careful how you name your elements. Do not use the same name as the ID of another element.

    More Bugs, Quirks and Inconsistencies
Working...