4

Does anyone know, why jquery behaves like this:

var $test = $(window).add(document).add("body");
$test.is(window) -> false
$test.is(document) -> true
$test.is("body") -> true

How do I find out whether a jquery-object contains the window via "is"?

1 Answer 1

1
var $test = $(window).add(document).add("body");
$test.is(function(index, elements) {
    return this === window;
});

the jQuery "is()" method checks the nodeType of the elements being passed. Document has a nodeType of 9. Body has a nodeType of 1. Window has no nodeType, ergo "undefined" which evaluates to false. To get around this, build your own filtering function as illustrated above.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah...document and body both have the NodePrototype in their prototype chain and are "HTML-Nodes", window is completely different and extends the WindowProtptype. Thanks for the hint. Would be nice for jquery to include that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.