I need to check if an element contains a certain child class using JQUERY.
I tried:
if ($('#myElement').has('.myClass')) {
do work son
}
Didn’t work.
My html code is laid out like this:
<div id="myElement">
<img>
<span>something</span>
<span class="myClass">Hello</span>
</div>
The easiest way would be to search for
.myClassas a child of#myElement:If you only want first level children, you’d use
>Another way would be passing a selector to
findand checking for any results:Or for first level children only: