aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Resig <jeresig@gmail.com>2008-12-22 00:52:53 +0000
committerJohn Resig <jeresig@gmail.com>2008-12-22 00:52:53 +0000
commit6b090328643988869e2288a4874935680d8573ca (patch)
tree54ac9a37ae9ef7ea4ed41a65364e0372a3782d3d
parentc9dd5d997229919b8dd4cc402b7b7a465982071a (diff)
downloadjquery-6b090328643988869e2288a4874935680d8573ca.tar.gz
jquery-6b090328643988869e2288a4874935680d8573ca.zip
Added support for the new .closest() method (very useful for event delegation).
-rw-r--r--src/core.js11
-rw-r--r--test/unit/core.js8
2 files changed, 19 insertions, 0 deletions
diff --git a/src/core.js b/src/core.js
index 7ddb2c5b0..ff8114bb5 100644
--- a/src/core.js
+++ b/src/core.js
@@ -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
diff --git a/test/unit/core.js b/test/unit/core.js
index 536e58cf5..1ae2dfa5f 100644
--- a/test/unit/core.js
+++ b/test/unit/core.js
@@ -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')" );