How to search the children of a HTMLDivElement?
Demo: http://jsfiddle.net/ThinkingStiff/y9K9Y/
If the <div>
you're searching for has a class, you can use getElementsByClassName()
:
document.getElementById( 'parentDiv' ).getElementsByClassName( 'childDiv' )[0];
If it doesn't have a class you can use getElementsByTagName()
:
document.getElementById( 'parentDiv' ).getElementsByTagName( 'div' )[0];
And if it has an id
you can, of course, just use getElementById()
to find it no matter where it is in the DOM:
document.getElementById( 'childDiv' );
//For immediate children
var children = document.getElementById('id').childNodes;
//or for all descendants
var children = document.getElementById('id').getElementsByTagName('*');