Element Object
The firstChild property returns the first child node of the selected element
If the selected node has no children, this property returns NULL.
Note: Firefox, and most other browsers, will treat empty white-spaces or new lines as text nodes, Internet Explorer will not. So, in the example below, we have a function that checks the node type of the first child node.
Element nodes has a nodeType of 1, so if the first child node is not an element node, it moves to the next node, and checks if this node is an element node. This continues until the first child node (which must be an element node) is found. This way, the result will be correct in all browsers.
Tip: To read more about the differences between browsers, visit our DOM Browsers chapter in our XML DOM Tutorial.
The following code fragment loads "books.xml" into xmlDoc using loadXMLDoc() and gets the first child node:
The output of the code above will be:
Element Object
The perfect solution for professionals who need to balance work, family, and career building.
More than 10 000 certificates already issued!
The HTML Certificate documents your knowledge of HTML.
The HTML5 Certificate documents your knowledge of advanced HTML5.
The CSS Certificate documents your knowledge of advanced CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The jQuery Certificate documents your knowledge of jQuery.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
Your message has been sent to W3Schools.