Let's say I'm writing a jQuery extension method. This method should climb the ancestor tree of an element until it reaches the document's root <html>
tag, at which point it should stop. I've implemented this as shown here:
$.fn.foo = function() {
var $foo = this;
while($foo[0] !== $(document).children()[0]) {
// do stuff with $foo
$foo = $foo.parent();
}
// do stuff
};
My question is: is there a better way than $foo[0] !== $(document).children()[0]
to know whether I've reached the root <html>
tag?