diff --git a/Readme.md b/Readme.md
index 29dba03c5f..13271d06de 100644
--- a/Readme.md
+++ b/Readme.md
@@ -586,6 +586,20 @@ $.html()
//
```
+#### .appendTo( target )
+Insert every element in the set of matched elements to the end of the target.
+
+```js
+$('
Plum').appendTo('#fruits')
+$.html()
+//=>
+// - Apple
+// - Orange
+// - Pear
+// - Plum
+//
+```
+
#### .prepend( content, [content, ...] )
Inserts content as the *first* child of each of the selected elements.
@@ -600,6 +614,20 @@ $.html()
//
```
+#### .prependTo( target )
+Insert every element in the set of matched elements to the beginning of the target.
+
+```js
+$('Plum').prependTo('#fruits')
+$.html()
+//=>
+// - Plum
+// - Apple
+// - Orange
+// - Pear
+//
+```
+
#### .after( content, [content, ...] )
Insert content next to each element in the set of matched elements.
@@ -614,7 +642,7 @@ $.html()
//
```
-#### .insertAfter( content )
+#### .insertAfter( target )
Insert every element in the set of matched elements after the target.
```js
@@ -642,7 +670,7 @@ $.html()
//
```
-#### .insertBefore( content )
+#### .insertBefore( target )
Insert every element in the set of matched elements before the target.
```js
diff --git a/lib/api/manipulation.js b/lib/api/manipulation.js
index 7e7670a16a..670837d3dc 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 (!target.cheerio) {
+ target = this.constructor.call(this.constructor, target, null, this._originalRoot);
+ }
+
+ target.append(this);
+
+ return this;
+};
+
+exports.prependTo = function(target) {
+ if (!target.cheerio) {
+ 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..0d4ba2a68d 100644
--- a/test/api/manipulation.js
+++ b/test/api/manipulation.js
@@ -496,6 +496,66 @@ describe('$(...)', function() {
});
});
+ describe('.appendTo', function() {
+
+ it('(html) : should add element as last child', function() {
+ var $plum = $('Plum').appendTo(fruits);
+ expect($plum.parent().children().eq(3).hasClass('plum')).to.be.ok();
+ });
+
+ it('($(...)) : should add element as last child', function() {
+ $('Plum').appendTo($fruits);
+ expect($fruits.children().eq(3).hasClass('plum')).to.be.ok();
+ });
+
+ it('(Node) : should add element as last child', function() {
+ $('Plum').appendTo($fruits[0]);
+ expect($fruits.children().eq(3).hasClass('plum')).to.be.ok();
+ });
+
+ it('(selector) : should add element as last child', function() {
+ $('Plum').appendTo('#fruits');
+ expect($fruits.children().eq(3).hasClass('plum')).to.be.ok();
+ });
+
+ it('(Array) : should add element as last child of all elements in the array', function() {
+ var $multiple = $('');
+ $('Plum').appendTo($multiple.get());
+ expect($multiple.first().children().eq(1).hasClass('plum')).to.be.ok();
+ expect($multiple.last().children().eq(1).hasClass('plum')).to.be.ok();
+ });
+ });
+
+ describe('.prependTo', function() {
+
+ it('(html) : should add element as first child', function() {
+ var $plum = $('Plum').prependTo(fruits);
+ expect($plum.parent().children().eq(0).hasClass('plum')).to.be.ok();
+ });
+
+ it('($(...)) : should add element as first child', function() {
+ $('Plum').prependTo($fruits);
+ expect($fruits.children().eq(0).hasClass('plum')).to.be.ok();
+ });
+
+ it('(Node) : should add node as first child', function() {
+ $('Plum').prependTo($fruits[0]);
+ expect($fruits.children().eq(0).hasClass('plum')).to.be.ok();
+ });
+
+ it('(selector) : should add element as first child', function() {
+ $('Plum').prependTo('#fruits');
+ expect($fruits.children().eq(0).hasClass('plum')).to.be.ok();
+ });
+
+ it('(Array) : should add element as first child of all elements in the array', function() {
+ var $multiple = $('');
+ $('Plum').prependTo($multiple.get());
+ expect($multiple.first().children().eq(0).hasClass('plum')).to.be.ok();
+ expect($multiple.last().children().eq(0).hasClass('plum')).to.be.ok();
+ });
+ });
+
describe('.after', function() {
it('() : should do nothing', function() {
@@ -790,7 +850,7 @@ describe('$(...)', function() {
});
it('(Node) : should add element as previous sibling', function() {
- var plum = $('Plum');
+ var plum = $('Plum')[0];
$('.apple').before(plum);
expect($('.apple').prev().hasClass('plum')).to.be.ok();
});