Skip to content

Commit 02dd7c5

Browse files
cowboyrwaldron
authored andcommitted
jQuery#addBack supports an optional selector that can be used to filter the prior set before adding it back. Fixes #9800
1 parent 7f2cc46 commit 02dd7c5

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/traversing.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,9 @@ jQuery.fn.extend({
143143
jQuery.unique( all ) );
144144
},
145145

146-
addBack: function() {
147-
return this.add( this.prevObject );
146+
addBack: function( selector ) {
147+
var prior = this.prevObject;
148+
return this.add( selector == null ? prior : prior.filter(selector) );
148149
}
149150
});
150151

test/unit/traversing.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,12 @@ test("has(Arrayish)", function() {
403403
});
404404

405405
test("addBack()", function() {
406-
expect(4);
406+
expect(5);
407407
deepEqual( jQuery("#en").siblings().addBack().get(), q("sndp", "en", "sap"), "Check for siblings and self" );
408408
deepEqual( jQuery("#foo").children().addBack().get(), q("foo", "sndp", "en", "sap"), "Check for children and self" );
409409
deepEqual( jQuery("#sndp, #en").parent().addBack().get(), q("foo","sndp","en"), "Check for parent and self" );
410410
deepEqual( jQuery("#groups").parents("p, div").addBack().get(), q("qunit-fixture", "ap", "groups"), "Check for parents and self" );
411+
deepEqual( jQuery("#select1 > option").filter(":first-child").addBack(":last-child").get(), q("option1a", "option1d"), "Should contain the last elems plus the *filtered* prior set elements" );
411412
});
412413

413414
test("siblings([String])", function() {

0 commit comments

Comments
 (0)