The documentation for jquery's closest
says the following:
.closest( selector [, context ] )
...
context
Type: Element
A DOM element within which a matching element may be found. If no context is passed in then the context of the jQuery set will be used instead.
As I understand it, the bolded text means that the two statements should be equivalent:
set.closest("a");
set.closest("a", set.context);
where set
is some jquery set.
However, this does not seem to be the case:
var context = $("#inner")[0];
var set = $("#el", context);
// the set's context is correctly the "inner" element
set.text("context: " + set.context.id);
// if the set's context is used, this closest should match nothing, but it matches and sets the color
set.closest("#outer").css("color", "red");
// with the context explicitly set, the "outer" is not found and no background color is set
set.closest("#outer", set.context).css("background-color", "blue");
#outer{
width: 100px;
height: 100px;
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="outer">
<div id="inner">
<div id="el"></div>
</div>
</div>
As you can see, when no context is explicitly set, the set's context does not seem to be used as the #outer
element is found by closest
. When explicitly set, the #outer
is correctly not found.
Is the documentation just incorrect or am I missing something?