How can I determine whether this clicked element is a subset of somemenu ?
var clicked = e.target;
// this checks see if the clicked has id of somemenu.
$(clicked).attr("id") == '#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.
This will work:
if ($(clicked).parents('#somemenu').length) {
// I am a child of somemenu so do stuff.
}
it's more easy work with stopPropagation
and the method on
of jquery:
jQuery('#myID').on('click',"*",function(e){
e.stopPropagation(); //stop the propagation
console.log("Clicked element: ",jQuery(this)); //get the element clicked by the mouse
console.log("Parent: ",jQuery(this).closest("#myID"));
});
if you don't stop the propagation you should call to all parents of the jQuery(this);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With