Check if Element is inside Another Specific Element
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
if($(' #childElementID' parentElementId).length == 1) { // YES, the child element is inside the parent } else { // NO, it is not inside }Using jQuery context !
missing the coma sorry !
if($(' #childElementID' , parentElementId).length == 1) { // YES, the child element is inside the parent } else { // NO, it is not inside }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
}