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 }
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".
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");
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.
if ($(".toDiv").find(".on").length > 0){ ///do something }
or
if ($(".toDiv .on").length > 0){ ///do something }
$('div.toDiv').each(function() { if($('div.on', this).length > 0) { //do something with this } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With