Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added support for the new .closest() method (very useful for event de…
…legation).
  • Loading branch information
jeresig committed Dec 22, 2008
1 parent c9dd5d9 commit 6b09032
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/core.js
Expand Up @@ -338,6 +338,17 @@ jQuery.fn = jQuery.prototype = {
}) ), "filter", selector );
},

closest: function( selector ) {
return this.map(function(){
var cur = this;
while ( cur && cur.ownerDocument ) {
if ( jQuery(cur).is(selector) )
return cur;
cur = cur.parentNode;
}
});
},

not: function( selector ) {
if ( typeof selector === "string" )
// test special case where just one selector is passed in
Expand Down
8 changes: 8 additions & 0 deletions test/unit/core.js
Expand Up @@ -1309,6 +1309,14 @@ test("filter()", function() {
equals( j.filter("[name]").length, 0, "Check node,textnode,comment to filter the one span" );
});

test("closest()", function() {
expect(4);
isSet( jQuery("body").closest("body").get(), q("body"), "closest(body)" );
isSet( jQuery("body").closest("html").get(), q("html"), "closest(html)" );
isSet( jQuery("body").closest("div").get(), [], "closest(div)" );
isSet( jQuery("#main").closest("span,#html").get(), q("html"), "closest(span,#html)" );
});

test("not()", function() {
expect(8);
equals( jQuery("#main > p#ap > a").not("#google").length, 2, "not('selector')" );
Expand Down

0 comments on commit 6b09032

Please sign in to comment.