diff --git a/src/manipulation.js b/src/manipulation.js index 37a8cfdfe..8c188c2d5 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -233,6 +233,12 @@ jQuery.fn.extend({ // this can help fix replacing a parent with child elements if ( !jQuery.isFunction( value ) ) { value = jQuery( value ).detach(); + + } else { + return this.each(function(i) { + var self = jQuery(this), old = self.html(); + self.replaceWith( value.call( this, i, old ) ); + }); } return this.each(function() { diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index bbf9a3a25..168c17c94 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -720,6 +720,16 @@ test("replaceWith(String|Element|Array<Element>|jQuery)", function() { test("replaceWith(Function)", function() { testReplaceWith(functionReturningObj); + + expect(16); + + var y = jQuery("#yahoo")[0]; + + jQuery(y).replaceWith(function(){ + equals( this, y, "Make sure the context is coming in correctly." ); + }); + + reset(); }) test("replaceAll(String|Element|Array<Element>|jQuery)", function() {