Visit Mozilla.org

DOM:element.nodeName

From MDC

« Gecko DOM Reference

Contents

[edit] Summary

Returns the name of the current node as a string.

[edit] Syntax

var str = node.nodeName;

str is a string variable storing the name of the current element.

nodeName is a read-only attribute.

[edit] Notes

Here are the returned values for different types of node.

Interface nodeName
Attr same as Attr.name
CDATASection "#cdata-section"
Comment "#comment"
Document "#document"
DocumentFragment "#document-fragment"
DocumentType same as DocumentType.name
Element same as Element.tagName
Entity entity name
EntityReference name of entity reference
Notation notation name
ProcessingInstruction same as ProcessingInstruction.target
Text "#text"

[edit] Example

Given the following markup:

<div id="d1">hello world</div>
<input type="text" id="t"/>

and the following script:

var div1 = document.getElementById("d1");
var text_field = document.getElementById("t"); 
text_field.value = div1.nodeName;

In XHTML (or any other XML format), text_field's value would read "div". However, in HTML, text_field's value would read "DIV".

Note that tagName property could have been used instead, since nodeName has the same value as tagName for an element. Bear in mind, however, that nodeName will return #text for text nodes while tagName will return undefined.

[edit] Specification

DOM Level 2 Core: Node.nodeName

DOM Level 3 Core: Node.nodeName