92

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');
}
Ben
  • 54,723
  • 49
  • 178
  • 224
James
  • 42,081
  • 53
  • 136
  • 161

11 Answers11

145

Something like this:

if(this.tagName == 'DIV') {
    alert("It's a div!");
} else {
    alert("It's not a div! [some other stuff]");
}
Roy
  • 7,811
  • 4
  • 24
  • 47
Bjorn
  • 5,272
  • 1
  • 24
  • 35
  • 4
    did you know that `$(this).get(0)` is equivalent to `this` but without the overhead? – Mic Jun 29 '11 at 10:02
  • 3
    @Bjorn Right, and neither is `this`. `$(this).get(0)` takes `this` (a normal JS DOM node), converts it to a jQuery object, runs `.get(0)`, which selects the first regular DOM node within the jQuery object... that is, takes you back to where you started. `this.tagName = $(this)[0].tagName = $(this).get(0).tagName`. – brymck Jun 29 '11 at 10:25
  • 13
    @Bryan, it's appalling how people think in jQuery and not anymore in Javascript – Mic Jun 29 '11 at 10:58
  • @Bjorn: Of course this does not work. In your example, you pass a string (selector), but in the answer, `this` is already a DOM element. Just try `this === $(this).get(0)`. – Felix Kling Jun 29 '11 at 14:09
  • 1
    Tripple equal is more correct when you know the type :) – Ben Apr 17 '15 at 05:34
  • 6
    Worth reminding to use .toLowerCase(), in case javascript decides to randomly return upper case names, even if the names are lower case in the html. – Buffalo Aug 20 '15 at 08:37
  • @Mic and so are you, jQuery is JavaScript. You just seem to have a better understanding what `this` means and `$` stands for ;) – dbf Nov 11 '17 at 21:03
  • @dbf, have a look at the edit history of the answer, and you will understand my...wow... already 6 years old comment =D – Mic Nov 12 '17 at 21:39
  • @Mic Yes! SO App needs debugging and in need of proof that questions are showing up in wrong places or shouldn't show up at all. Even after 10 years, the comment is still valid, unless jqueryscript is given its own compiler, which I hope, sincerely, is very unlikely .. cheers anyway ;) – dbf Nov 12 '17 at 21:46
48

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

$(this).is("div,ul,blockquote")
MBO
  • 30,379
  • 5
  • 50
  • 52
22

To check if this element is DIV

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

Same for HTMLUListElement for UL,
HTMLQuoteElement for blockquote

nikos.svnk
  • 1,375
  • 1
  • 13
  • 24
Andreq Frenkel
  • 1,188
  • 9
  • 14
21

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)

Community
  • 1
  • 1
Mic
  • 24,812
  • 9
  • 57
  • 70
5

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');

gztomas
  • 3,030
  • 3
  • 27
  • 38
5
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

Setthase
  • 13,988
  • 2
  • 27
  • 30
Dirk McQuickly
  • 2,099
  • 1
  • 17
  • 19
3

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.

Rup
  • 33,765
  • 9
  • 83
  • 112
2

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");
  }
});
brymck
  • 7,555
  • 28
  • 31
1
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 */
Ir Calif
  • 460
  • 6
  • 7
1

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

GullerYA
  • 1,320
  • 14
  • 27
0

Try using tagName

Giann
  • 3,142
  • 3
  • 23
  • 33