KJW KJW - 1 year ago 61
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.-->

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

Answer Source

This will work:

if ($(clicked).parents('#somemenu').length) {
    // I am a child of somemenu so do stuff.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download