Check if element is a div

JavascriptJqueryHtmlCss

Javascript Problem Overview


How do I check if $(this) is a div, ul or blockquote?

For example:

if ($(this) is a div) {
  alert('its a div!');
} else {
  alert('its not a div! some other stuff');
}

Javascript Solutions


Solution 1 - Javascript

Something like this:

if(this.tagName == 'DIV') {
    alert("It's a div!");
} else {
    alert("It's not a div! [some other stuff]");
}

Solution 2 - Javascript

Solutions without jQuery are already posted, so I'll post solution using jQuery

$(this).is("div,ul,blockquote")

Solution 3 - Javascript

Without jQuery you can say this.tagName === 'DIV'

Keep in mind that the 'N' in tagName is uppercase.

Or, with more tags:

/DIV|UL|BLOCKQUOTE/.test(this.tagName)

Solution 4 - Javascript

To check if this element is DIV

if (this instanceof HTMLDivElement) {
   alert('this is a div');
}

Same for HTMLUListElement for UL,
HTMLQuoteElement for blockquote

Solution 5 - Javascript

if(this.tagName.toLowerCase() == "div"){
	//it's a div
} else {
	//it's not a div
}

edit: while I was writing, a lot of answers were given, sorry for doublure

Solution 6 - Javascript

Going through jQuery you can use $(this).is('div'):

> Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.

Solution 7 - Javascript

Some of these solutions are going a bit overboard. All you need is tagName from regular old JavaScript. You don't really get any benefit from re-wrapping the whole thing in jQuery again, and especially running some of the more powerful functions in the library to check the tag name. If you want to test it on this page, here's an example.

$("body > *").each(function() {
  if (this.tagName === "DIV") {
    alert("Yeah, this is a div");
  } else {
    alert("Bummer, this isn't");
  }
});

Solution 8 - Javascript

let myElement =document.getElementById("myElementId");

if(myElement.tagName =="DIV"){

  alert("is a div");

}else{

  alert("is not a div");

}
/*What ever you may need to know the type write it in capitalised letters "OPTIO" ,"PARAGRAPH", "SPAN" AND whatever */

Solution 9 - Javascript

I'm enhancing the answer of Andreq Frenkel, just wanted to add some and it became too lengthy so gone here...

Thinking about CustomElements extending the existing ones and still being able to check if an element is, say, input, makes me think that instanceof is the best solution for this problem.

One should be aware though, that instanceof uses referential equality, so HTMLDivElement of a parent window will not be the same as the one of its iframe (or shadow DOM's etc).

To handle that case, one should use checked element's own window's classes, something like:

element instanceof element.ownerDocument.defaultView.HTMLDivElement

Solution 10 - Javascript

Old question but since none of the answers mentions this, a modern alternative, without jquery, could be just using a CSS selector and Element.matches()

element.matches('div, ul, blockquote');

Solution 11 - Javascript

Try using tagName

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJamesView Question on Stackoverflow
Solution 1 - JavascriptBjornView Answer on Stackoverflow
Solution 2 - JavascriptMBOView Answer on Stackoverflow
Solution 3 - JavascriptMicView Answer on Stackoverflow
Solution 4 - JavascriptAndreq FrenkelView Answer on Stackoverflow
Solution 5 - JavascriptDirk McQuicklyView Answer on Stackoverflow
Solution 6 - JavascriptRupView Answer on Stackoverflow
Solution 7 - JavascriptbrymckView Answer on Stackoverflow
Solution 8 - JavascriptIr CalifView Answer on Stackoverflow
Solution 9 - JavascriptGullerYAView Answer on Stackoverflow
Solution 10 - JavascriptgztomasView Answer on Stackoverflow
Solution 11 - JavascriptGiannView Answer on Stackoverflow