Latest web development tutorials

XML DOM lastChild property

Node Object Reference Node objects

Definition and Usage

lastChild property returns the specified node's last child.

grammar

nodeObject.lastChild


Tips and Notes

Note: Firefox and most other browsers, the empty spaces between the nodes will generate or wrap as text nodes, while Internet Explorer will ignore whitespace text nodes between nodes generated.Thus, in the example below, we'll use a function that checks the node type of the last child node.

Node type element node is 1, so if the last child node is not an element node, it will move to the previous node, and checks if this node is an element node. The whole process continues until the last child element node is found. Through this method, we can get the right results in all browsers.

Tip: For more information about browser differences, please visit us in our XML DOM tutorial DOM browser section.


Examples

The following code fragment uses loadXMLDoc () to " the Books.xml " into xmlDoc and displays the node name and the last child node of the document type:

Examples

//check if the last node is an element node
function get_lastchild(n)
{
x=n.lastChild;
while (x.nodeType!=1)
{
x=x.previousSibling;
}
return x;
}

xmlDoc=loadXMLDoc("books.xml");

x=get_lastchild(xmlDoc);
document.write("Nodename: " + x.nodeName);
document.write(" (nodetype: " + x.nodeType);

Output:

Nodename: bookstore (nodetype: 1)

try it"

Try Demos

Get the first child node of the document


Node Object Reference Node objects