XML DOM localName Property

Definition and Usage

The localName property returns the local name (element name) of the selected element.

If the selected node is not an element or attribute, this property returns NULL.

Syntax:

elementNode.localName

Instance

In all the examples, we will use XML files books.xmland the JavaScript function loadXMLDoc().

Example 1

The following code snippet retrieves the local name of the first <book> element from "books.xml":

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0];
document.write(x.localName);

The output of the above code is:

book

Example 2

The following code snippet retrieves the local name of the last <book> element from "books.xml":

//check if the last node is an element node
function get_lastchild(n)
{
var x=n.lastChild;
while (x.nodeType!=1)
  {
  x=x.previousSibling;
  }
return x;
}
xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.documentElement;
var lastNode=get_lastchild(x);
document.write(lastNode.localName);

The output of the above code is:

book