Difference between .tagName and .nodeName

Kdniazi picture Kdniazi · Feb 2, 2011 · Viewed 47.8k times · Source

What is the difference between $('this')[0].nodeName and $('this')[0].tagName?

Answer

user113716 picture user113716 · Feb 2, 2011

The tagName property is meant specifically for element nodes (type 1 nodes) to get the type of element.

There are several other types of nodes as well (comment, attribute, text, etc.). To get the name of any of the various node types, you can use the nodeName property.

When using nodeName against an element node, you'll get its tag name, so either could really be used, though you'll get better consistency between browsers when using nodeName.