Replace the first selector with the child you are testing and the second selector with the parent you are testing for.
if ( $(".child-element").parents("#main-nav").length == 1 ) {
// YES, the child element is inside the parent
} else {
// NO, it is not inside
}
:D
Thanks! Clean and Short
Using jQuery context !
missing the coma sorry !
Hey Chris! thanks for this…! my method was only detecting the first parent and not the ancestors :(
if($(“.parentElement”).has(“.childClass”).length)){
//do something
}
else{
//do something
}
99% of the time, there is NO need for else. Simply a break or a continue.
If (!negatedCondition) {
// do some logic
// return/continue/nothing
}
// do some logic
return
thanks sajay . I am finding this
just use the method in jquery ,
$.contains(param1,param2);
param1 is the container which also called parent , this param must be a DOM element .
param2 is the element contained by the containner .
this method could find out the container-contained relationship weather it’s the direct child of nested more deeply .