Home Ask Login Register

Developers Planet

Your answer is one click away!

Ehsan February 2016

Instead of $.closest in Angularjs

I want to implement this code in directive but in jqLite doesn't this function. what should I do?

 var thisCell = element.closest('.sampleclass');


TeoMor February 2016

The documentation states that:

For lookups by tag name, try instead angular.element(document).find(...) or $document.find(), or use the standard DOM APIs, e.g. document.querySelectorAll().

sp00m February 2016

You have to implement this functionality manually, for example:

var thisCell = (function closest(e, className) {
  if (e[0].nodeName == "HTML") {
    return null;
  } else if (e.hasClass(className)) {
    return e;
  } else {
    return closest(e.parent(), className);
})(element, "sampleclass");

Post Status

Asked in February 2016
Viewed 3,149 times
Voted 10
Answered 2 times


Leave an answer

Quote of the day: live life