JavaScript: Node Name

By Xah Lee. Date: .
node.nodeName
Return the node's (tag) name (a string), if the node is a element node (nodeType is 1). If it's a text node, the value is "#text".

[see HTML/CSS Case Sensitivity]

You can use str.toLowerCase() to convert it.

// first paragraph
document.getElementsByTagName("p")[0].nodeName // "P"

// first paragraph's content
document.getElementsByTagName("p")[0].childNodes[0].nodeName // "#text"

As of 2012-04-29, this works in all major browsers.

JavaScript/DOM: Node Name, Node Type, Node Value

BUY Ξ£JS JavaScript in Depth