document.getElementById() will not work if node was created on the fly and not yet attached to the main dom document.
For example, using Ajax, not all nodes are attached at any given point. In this case, you need to either explicitly track the descriptor for each node (usually best for performance), or use something like this to look at the objects:
function domGet( id , rootNode ) {
if ( !id ) return null;
if ( rootNode === undefined ) {
var o = document.getElementById( id );
return o;
} else {
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;
}
}
var childNodes = node.childNodes;
if ( childNodes && childNodes.length > 0 ) {
for ( var j = 0 ; j < childNodes.length; j++ ) {
children.push( childNodes[j] );
}
}
}
nodes = children;
}
return null;
}
}
source
share