Say I'm writing a jQuery extension method. This method should climb the ancestor tree of the element until it reaches the <html>document root tag , after which it should stop. I implemented this as shown below:
$.fn.foo = function() {
var $foo = this;
while($foo[0] !== $(document).children()[0]) {
$foo = $foo.parent();
}
};
My question is: is there a better way than $foo[0] !== $(document).children()[0]knowing if I have reached the root tag <html>?
source
share