Javascript Reference
Categories: Node

javascript Node firstChild, lastChild

@April 4, 2009, 2:51 a.m.
firstChild, lastChild Firefox/Netscape/NN 6 IE 5 Chrome/Safari/DOM 1  

 

Read-only  

Return a reference to the first or last child node (respectively) of the current element node. Most commonly, these child nodes are text nodes nested inside an element. For a simple element containing only one text node, both properties return a reference to the same text node. More complex constructions, such as tr elements, can have other element nodes (td elements) as their child nodes, but some browsers may turn source code carriage returns between elements into text nodes. Therefore, it's a good idea to validate the type of node returned by either property before acting on it.

 
Example
 
if (document.getElementById("elementID").firstChild.nodeType == 3) {
 // process as a text node
}
 
Value

Node object (including text node, HTML element node, etc.) reference.

 
Default

null


Powered by Linode.