Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if element is a div

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'); } 
like image 568
James Avatar asked Jun 29 '11 09:06

James


People also ask

How do I check if a div element is empty?

Use the childNodes property to check if a div element is empty. The childNodes property returns a NodeList of the element's child nodes, including elements, text nodes and comments. If the property returns a value of 0 , then the div is empty.

Is div a HTML element?

The <div> tag defines a division or a section in an HTML document. The <div> tag is used as a container for HTML elements - which is then styled with CSS or manipulated with JavaScript. The <div> tag is easily styled by using the class or id attribute.


2 Answers

Something like this:

if(this.tagName == 'DIV') {     alert("It's a div!"); } else {     alert("It's not a div! [some other stuff]"); } 
like image 97
Bjorn Avatar answered Sep 21 '22 14:09

Bjorn


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

$(this).is("div,ul,blockquote") 
like image 41
MBO Avatar answered Sep 20 '22 14:09

MBO