getElementById doesn't work on a node

17,412

Solution 1

.getElementById() is on document, like this:

document.getElementById("2");

Since IDs are supposed to be unique, there's no need for a method that finds an element by ID relative to any other element (in this case, inside that parent). Also, they shouldn't start with a number if using HTML4, a numberic ID is valid in HTML5.

Solution 2

replace .getElementById(id) with .querySelector('#'+id);

Solution 3

document.getElementById() won't work if the node was created on the fly and not yet attached into the main document dom.

For example with Ajax, not all nodes are attached at any given point. In this case, you'd either need to explicitly track a handle to each node (generally best for performance), or use something like this to look the objects back up:

function domGet( id , rootNode ) {
    if ( !id ) return null;

    if ( rootNode === undefined ) {

        // rel to doc base
        var o = document.getElementById( id );
        return o;

    } else {

        // rel to current node
        var nodes = [];
        nodes.push(rootNode);
        while ( nodes && nodes.length > 0 ) {
            var children = [];
            for ( var i = 0; i<nodes.length; i++ ) {
                var node = nodes[i];
                if ( node && node['id'] !== undefined ) {
                    if ( node.id == id ) {
                        return node; // found!
                    }
                }

                // else keep searching
                var childNodes = node.childNodes;
                if ( childNodes && childNodes.length > 0 ) {
                    for ( var j = 0 ; j < childNodes.length; j++ ) {
                        children.push( childNodes[j] );
                    }
                }
            }
            nodes = children;
        }

        // nothing found
        return null;
    }
}
Share:
17,412
Ursus Russus
Author by

Ursus Russus

Updated on June 09, 2022

Comments

  • Ursus Russus
    Ursus Russus almost 2 years

    In this simple script i get the error "obj.parentNode.getElementById is not a function", and I have no idea, what is wrong.

    <script type="text/javascript">
    
            function dosomething (obj) {
             sibling=obj.parentNode.getElementById("2");
             alert(sibling.getAttribute("attr"));
            }
    
    </script>
    
    <body>
     <div>
      <a id="1" onclick="dosomething(this)">1</a>
      <a id="2" attr="some attribute">2</a>
     </div>
    </body>
    
  • Ursus Russus
    Ursus Russus over 13 years
    So, despite getElementById is often described together with other DOM methods as appliable to any node, in fact it's appliable only to document. OK, thanks.
  • Arajay
    Arajay over 2 years
    This answer actually solves the initial problem even if it does not answer the underlying question.
  • syahid246
    syahid246 about 2 years
    querySelector is not supported in IE 6 - 7, see in querySelector Can I use