Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: check if a div contains a div with class on

Tags:

jquery

I have several <div> elements inside another <div>.

I want to do an if statement which is true if a div inside the top div contains class on.

HTML:

<div class="toDiv">     <div>      </div>     <div class="on">      </div> </div> 

jQuery:

if ($(".toDiv").contains("on")) {     // do something } 
like image 974
Beginner Avatar asked Sep 03 '12 15:09

Beginner


People also ask

How do you check if a div contains a class in jQuery?

jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".

How can check CSS property value in jQuery?

Get a CSS Property Value You can get the computed value of an element's CSS property by simply passing the property name as a parameter to the css() method. Here's the basic syntax: $(selector). css("propertyName");

How do you target a class in jQuery?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.


2 Answers

if ($(".toDiv").find(".on").length > 0){    ///do something } 

or

if ($(".toDiv .on").length > 0){    ///do something } 
like image 177
Claudio Redi Avatar answered Oct 09 '22 02:10

Claudio Redi


$('div.toDiv').each(function() {     if($('div.on', this).length > 0) {         //do something with this     } }); 
like image 30
Ropstah Avatar answered Oct 09 '22 02:10

Ropstah