-1

I have the access to my parent element like this:

var parentElem = angular.element('#parent');

Then on this below click event I want to check if the click is generated by any child of above parent.

$('html').click(function (e) {

  // check e.target is child of 'parentElem'           

});
Shardul
  • 984
  • 1
  • 12
  • 19

1 Answers1

1

You should be able to achieve this with plain vanilla JavaScript:

// Get a reference to a raw DOM node
var parentElem = angular.element("#parent")[0];

$("html").click(function(e) {
    // Get a reference to the event target
    var element = e.target;

    // "Climb" up the document tree until you've found the parent,
    // or you can "climb" up no further.
    while (parentElem !== element && element.parentNode) {
        element = element.parentNode;
    }

    if (element === parentElem) {
        // event target is inside parentElem
    }
    else {
        // event target is not a child of parentElem
    }
});
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92