nodeName (W3C DOM Core property)
| Version | Depr. | Static | Read-only |
|---|---|---|---|
| DOM1 | No | No | Yes |
| IE6+ | FF1.5+ | SA3+ | OP9+ |
|---|---|---|---|
| Full | Full | Full | Full |
Example
var nodename = node.nodeName;
In the example above, the nodename variable will be the name of node, depending on its type, as listed above.
So if we take this HTML example:
<p> They used to call me <cite class="nickname">One Eyed Jim</cite> — because there's only one <q>I</q> in <q>Jim</q> </p>
The outer p element would have the
node name P, the cite element would
have the node name CITE, its
class attribute would have the node name
class, and the text inside it would have the node
name #text.
Since the case of the returned value depends on whether the
document is HTML or XML (which includes XHTML mode1), it's often
helpful to run this value through JavaScript's
toLowerCase function, to get a value with
predictable case. So if we did this:
var nodename = node.nodeName.toLowerCase();
Then the nodename variable in the first example
would then have the value p, regardless of the
document type.
Description
The name of this node, according to its type:
| Interface | nodeName |
|---|---|
Element |
The tag name, eg. HTML |
Attr |
The attribute name, eg. id |
Text |
#text |
CDATASection |
#cdata-section |
EntityReference |
The name of the entity reference, eg.
amp |
Entity |
The entity name, eg. & |
ProcessingInstruction |
The target of the processing instruction, eg.
xml-stylesheet |
Comment |
#comment |
Document |
#document |
DocumentType |
The name of the document type, eg.
html |
DocumentFragment |
#document-fragment |
Notation |
The notation name |
DOM 1 methods such as getAttribute are not namespace aware, and
identify nodes by their nodeName. DOM 2 methods
such as getAttributeNS identify nodes by a
combination of their namespaceURI and localName; these two properties combined form a
qualified name — a name which includes the
namespace prefix and the local name, delimited with a
colon; thus the nodeName property of a namespaced
element or attribute will return this qualified name.
In HTML, element names are returned in all uppercase, and attribute names in all lowercase, regardless of the case used in the document; in XML the case used in the document is preserved.
This property is read-only.
Compatibility
| Internet Explorer | Firefox | Safari | Opera | |||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 5.5 | 6.0 | 7.0 | 1.5 | 2.0 | 3.0 | 1.3 | 2.0 | 3.0 | 9.0 | 9.5 |
| Buggy | Full | Full | Full | Full | Full | Partial | Partial | Full | Full | Full |
In
Internet Explorer 5.5 in HTML it's only
possible to refer to an attribute's value, not to the node
itself, however attribute values retrieved through the attributes collection do inherit the
nodeName and nodeValue
properties. This browser also identifies Comment
nodes as Element nodes (they return the
nodeName !); and it cannot
retrieve the nodeName of a Document (it has an object reference, but the
nodeName is undefined).
In
Safari 1.3 and 2 in XHTML mode2 a static namespaced node
returns a nodeName that does not include its
namespace prefix; ie. it returns the localName. A
created namespaced element (creating using createElementNS) returns the correct
nodeName.
In Safari 1.3 and 2
in HTML mode3 a created namespaced element returns a
nodeName in which the local name part is in the
canonical uppercase form, but the prefix part is in the specified case
(resulting in a value such as dc:DATE). Also, they
cannot retrieve the nodeName of a Comment node, because they cannot see comments at all.
User-contributed notes
There are no comments yet.
Add a note
To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.