108

Is there a way to test the type of an element in JavaScript?

The answer may or may not require the prototype library, however the following setup does make use of the library.

function(event) {
  var element = event.element();
  // if the element is an anchor
  ...
  // if the element is a td
  ...
}
1
  • you are not asking for the Type. the type is probably an Object. What you ask for is the tag, as mentioned in the answers, the tagName property Commented Jan 4, 2022 at 7:10

7 Answers 7

136

You can use typeof(N) to get the actual object type, but what you want to do is check the tag, not the type of the DOM element.

In that case, use the elem.tagName or elem.nodeName property.

if you want to get really creative, you can use a dictionary of tagnames and anonymous closures instead if a switch or if/else.

77
if (element.nodeName == "A") {
 ...
} else if (element.nodeName == "TD") {
 ...
}
2
  • 1
    Sometimes it is. Anyway, you could always use element.nodeName.match(/\bTBODY\b/i) or element.nodeName.toLowerCase() == 'tbody' etc.
    – Robusto
    Commented Sep 27, 2012 at 16:05
  • 10
    @Robusto is incorrect. If the document is HTML and the DOM implementation is correct, it will always be uppercase. According to: w3.org/TR/DOM-Level-3-Core/core.html#ID-104682815 under the "tagName" section (for elements nodeName == tagName) "The HTML DOM returns the tagName of an HTML element in the canonical uppercase form, regardless of the case in the source HTML document." Commented Jan 29, 2014 at 14:57
21

Perhaps you'll have to check the nodetype too:

if(element.nodeType == 1){//element of type html-object/tag
  if(element.tagName=="a"){
    //this is an a-element
  }
  if(element.tagName=="div"){
    //this is a div-element
  }
}

Edit: Corrected the nodeType-value

5
  • 4
    Beware of the case of tagName. Commented Oct 9, 2008 at 4:01
  • @Casey: It will in an HTML page; in an XHTML page the case of the tag is preserved (so "a" will be "A" in HTML pages, and "a" in XHTML pages): w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/… (Assuming the XHTML page has been correctly served and not served as text/html.) Commented Apr 25, 2011 at 15:59
  • 2
    @T.J.Crowder so it looks like the best option is element.tagName.toLowerCase() === 'a'
    – p3drosola
    Commented Jun 30, 2012 at 22:41
  • @Ped: Yes, or element.nodeName.toLowerCase() if it's possible that element won't actually be an element (e.g., if you haven't done the nodeType == 1 check listed above). The Node interface has nodeName. For Element instances, it's the same as tagName. For other kinds of nodes, it's things like "#text" or "#document". I think I'd always use the nodeType check, though. Commented Jul 1, 2012 at 7:57
  • 2019 update: at least on modern Chromium (v79.0.3945.79) the tagname string is uppercase. "For DOM trees which represent HTML documents, the returned tag name is always in the canonical upper-case form. For example, tagName called on a <div> element returns "DIV" https://developer.mozilla.org/en-US/docs/Web/API/Element/tagName correct way to compare would be node.tagName == 'DIV'
    – marcs
    Commented Dec 18, 2019 at 5:58
6

roenving is correct BUT you need to change the test to:

if(element.nodeType == 1) {
//code
}

because nodeType of 3 is actually a text node and nodeType of 1 is an HTML element. See http://www.w3schools.com/Dom/dom_nodetype.asp

4

Although the previous answers work perfectly, I will just add another way where the elements can also be classified using the interface they have implemented.

Refer W3 Org for available interfaces

console.log(document.querySelector("#anchorelem") instanceof HTMLAnchorElement);
console.log(document.querySelector("#divelem") instanceof HTMLDivElement);
console.log(document.querySelector("#buttonelem") instanceof HTMLButtonElement);
console.log(document.querySelector("#inputelem") instanceof HTMLInputElement);
<a id="anchorelem" href="">Anchor element</a>
<div id="divelem">Div Element</div>
<button id="buttonelem">Button Element</button>
<br><input id="inputelem">

The interface check can be made in 2 ways as elem instanceof HTMLAnchorElement or elem.constructor.name == "HTMLAnchorElement", both returns true

2

I usually get it from the toString() return value. It works in differently accessed DOM elements:

var a = document.querySelector('a');

var img = document.createElement('img');

document.body.innerHTML += '<div id="newthing"></div>';
var div = document.getElementById('newthing');

Object.prototype.toString.call(a);    // "[object HTMLAnchorElement]"
Object.prototype.toString.call(img);  // "[object HTMLImageElement]"
Object.prototype.toString.call(div);  // "[object HTMLDivElement]"

Then the relevant piece:

Object.prototype.toString.call(...).split(' ')[1].slice(0, -1);

It works in Chrome, FF, Opera, Edge, IE9+ (in older IE it return "[object Object]").

0

I have another way of testing the same.

Element.prototype.typeof = "element";
var element = document.body; // any dom element
if (element && element.typeof == "element"){
   return true; 
   // this is a dom element
}
else{
  return false; 
  // this isn't a dom element
}

1
  • extending the prototype is usually a bad idea. Commented Jan 4, 2022 at 7:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.