KJW KJW - 4 months ago 10
jQuery Question

How to determine if the clicked element is a child of some ID?

How can I determine whether this clicked element is a subset of somemenu ?

jQuery Example:



var clicked = e.target;

// this checks see if the clicked has id of somemenu.
$(clicked).attr("id") == '#somemenu';


HTML example of somemenu:



<div id="somemenu">
<a href="something.php"> menu 1 </a>
<!--bunch of other elements here, can be anything.-->
</div>


I want to catch any element that is subset of `div#somemenu? when it's clicked.

Answer

This will work:

if ($(clicked).parents('#somemenu').length) {
    // I am a child of somemenu so do stuff.
}