YouTip LogoYouTip

Prop Document Firstchild

# XML DOM firstChild Property * * Document Object](#) * * * ## Definition and Usage The firstChild property returns the first child node of the document. ## Syntax documentObject.firstChild * * * ## Tips and Notes **Note:** Firefox and most other browsers treat empty whitespace or line breaks between nodes as text nodes, while Internet Explorer ignores empty text nodes generated between nodes. Therefore, in the example below, we will use a function to check the node type of the first child node. The node type of an element node is 1, so if the first child node is not an element node, it moves to the next node and continues to check if this node is an element node. This process continues until the first element child node is found. Using this method, we can get the correct result in all browsers. **Tip:** To learn more about browser differences, visit our (#) section in our XML DOM Tutorial. * * * ## Example The following code snippet uses [loadXMLDoc()](#) to load "[books.xml](#)" into xmlDoc and displays the node name and node type of the first child node of the document: ## Example // Get the first child node name and value function get_firstchild(n){x=n.firstChild; while(x.nodeType!=1){x=x.nextSibling; }return x; }xmlDoc=loadXMLDoc("books.xml"); x=get_firstchild(xmlDoc); document.write("Nodename: " + x.nodeName); document.write(" nodetype: " + x.nodeType + "
"); Output: Nodename: bookstore (nodetype: 1) [Try it Β»](#) * * * ## Try it (#) * * Document Object](#)
← Dom Prop Document ImplementatiDom Met Node Insertbefore β†’