JS DOM: Node Name

By Xah Lee. Date: . Last updated: .
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".
  • For HTML document, the return value is usually all UPPER CASE. (regardless what's in the file.)
  • For XML/XHTML, it is always lowercase (because XML allow just lowercase.) However, there are edge cases, when a DOM element is imported from htm/html5/xml, or when older browser encounters a tag it doesn't know.

γ€”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"

JS DOM, About Node, Node Type