diff --git a/lib/api/manipulation.js b/lib/api/manipulation.js index 7e7670a16a..8947e87e5d 100644 --- a/lib/api/manipulation.js +++ b/lib/api/manipulation.js @@ -102,6 +102,26 @@ var uniqueSplice = function(array, spliceIdx, spliceCount, newElems, parent) { return array.splice.apply(array, spliceArgs); }; +exports.appendTo = function(target) { + if (typeof target === 'string') { + target = this.constructor.call(this.constructor, target, null, this._originalRoot); + } + + target.append(this); + + return this; +}; + +exports.prependTo = function(target) { + if (typeof target === 'string') { + target = this.constructor.call(this.constructor, target, null, this._originalRoot); + } + + target.prepend(this); + + return this; +}; + exports.append = _insert(function(dom, children, parent) { uniqueSplice(children, children.length, 0, dom, parent); }); diff --git a/test/api/manipulation.js b/test/api/manipulation.js index f5c823755d..b38c43f5ee 100644 --- a/test/api/manipulation.js +++ b/test/api/manipulation.js @@ -496,6 +496,24 @@ describe('$(...)', function() { }); }); + describe('.appendTo', function() { + + it('(Node) : should add element as last child', function() { + $('
  • Plum
  • ').appendTo($fruits); + expect($fruits.children().eq(3).hasClass('plum')).to.be.ok(); + }); + + }); + + describe('.prependTo', function() { + + it('(html) : should add element as first child', function() { + $('
  • Plum
  • ').prependTo($fruits); + expect($fruits.children().eq(0).hasClass('plum')).to.be.ok(); + }); + + }); + describe('.after', function() { it('() : should do nothing', function() {