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');
}
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');
}
Something like this:
if(this.tagName == 'DIV'){
alert("It's a div!");
} else {
alert("It's not a div! [some other stuff]");
}
$(this).get(0)
is equivalent to this
but without the overhead?
– Mic
Jun 29 '11 at 10:02
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
Solutions without jQuery are already posted, so I'll post solution using jQuery
$(this).is("div,ul,blockquote")
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)
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
$(this).tagName
is just plain wrong. You mean $(this).attr('tagName')
– Marco Faustinelli
Sep 13 '15 at 8:31
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.
To check if this element is DIV
if (this instanceof HTMLDivElement) {
alert('this is a div');
}
Same for HTMLUListElement
for UL,
HTMLQuoteElement
for blockquote
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");
}
});
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
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 */