Element.tagName (Property)

The name of the HTML tag that instantiated this object.

Availability:

DOM level - 1
JavaScript - 1.5
JScript - 3.0
Internet Explorer - 4.0
Netscape - 6.0
Opera - 5.0
Property/method value type:String primitive
JavaScript syntax:-myElement.tagName

This property contains a text string with the HTML tag name in upper case for the tag that instantiated the object. This is also some indication of the object class, which will help you to write generic functions that can operate on many kinds of objects in an intelligent way.

The tag name is returned without any left and right carets (greater/less than signs) and in fully DOM compliant implementations will be the same value as the class name of the object. There are a few inconsistencies that prevent this being used reliably as a class name for non DOM specified objects, but within some constraints, it may be useful in that context for determining what kind of object a script is operating on.

It is possible that the Element represents a text node. Text nodes are placed between the elements that represent HTML tags. We can then operate on the text in between adjacent HTML tags. With this capability, we probably don't need innerHTML, OuterHTML, innerText and outerText properties anymore. However, we will need to get a little bit smarter with our navigation of document trees to locate these text nodes.

If the element does represent a text node, then the tagName value is meaningless. Typically a browser will respond with an undefined value which could be difficult to handle. An empty space would have been easier to cope with. You should check the value you get back from this property, unless you are absolutely certain it relates to an HTML tag. If you suspect it might be a text node, then check it before using it or your script will crash.

See also:Attribute.nodeName, Element object

Property attributes:

ReadOnly.