This post will discuss how to determine the element’s type in JavaScript and jQuery.

1. Using JavaScript

The tagName property returns the element’s tag name in upper-case for HTML documents. For example, tagName returns DIV when called on a <div> element.

JS


HTML



Edit in JSFiddle

 
Alternatively, you can use the nodeName property that returns the current Node’s name, which is the same as tagName for an element.

JS


HTML



Edit in JSFiddle

 
Note when called on an <input> element, both the tagName or nodeName property returns INPUT, which doesn’t tell whether the input is a text box or a checkbox or a radio button. However, you can use the type attribute for this purpose:

JS


HTML



Edit in JSFiddle

2. Using jQuery

With jQuery, you can use the .prop() method to get the value of tagName or nodeName or type property.

JS


HTML



Edit in JSFiddle

 
If you just need to check for the specific type of element, you can use the .is() method, which returns a Boolean value:

JS


HTML



Edit in JSFiddle

That’s all about checking an element’s type in JavaScript and jQuery.