.appendTo()
JQ Home <<
Manipulation <<
.appendTo()
DOM insertion inside.
Description
The .appendTo()
method is used to insert each element in the matched set to the end of the specified content.
- The
.appendTo()
method inserts the specified content as the 'last child' of each element in the matched set. - Use the
.prependTo()
method to insert the specified content as the 'first child' of each element in the matched set. - The
.appendTo()
method works slightly differently when the content being appended is an existing element on the page:- When there is only one target element the content element will be moved and not cloned.
- When there is more than one target element the content element will be cloned to the target elements.
- Using the
.appendTo()
method this way has the side-effect of producing elements with duplicateid
attributes, which are supposed to be unique. Avoid cloning elements with theid
attribute or if absolutely necessary ensure elements that might be cloned are coded using theclass
attribute identifier instead.
- Using the
- The
.appendTo()
and.append()
methods perform the same tasks but using different syntax:- When using the
.appendTo()
method the content precedes the method, either as a selector expression or as markup created on the fly, and is inserted into the target container. - When using the
.append()
method the selector expression preceding the method is the container into which the content is inserted.
- When using the
Syntax
Signature | Description |
---|---|
.appendTo( target ) | Insert each element in the matched set to the end of the specified content. |
Parameters
Parameter | Description | Type |
---|---|---|
target | A DOM element, HTML string, or jQuery object to insert the matched set into. | Element ,HTMLstring orjQuery |
Return
A jQuery
object.
.appendTo( target )
Example
Manip. << Top
Insert each element in the matched set to the end of the specified content.
In the example below when the left button is pressed we append 'td' elements we created 'on the fly' to each table row within the table with a class of 'testtable2'.
When the centre button is pressed we first create a variable of the table with a class of 'testtable' table row 1 DOM element. We then select a single target to append this row to. As stated above in the description when only a single target exists and we are appending existing content, the elements are actually moved and not cloned to the target element. The table row is moved from the first to second table.
When the right button is pressed we first create a variable of the table with a class of 'testtable' table row 3 DOM element. We then select multiple targets to append this row to. As stated above in the description when multiple targets exist and we are appending existing content, the elements are cloned to the target elements. The table row is appended to the bottom of all other tables on the page.
Table Row 1 (id of trid1), Table Data 1 | Table Row 1 (id of trid1), Table Data 2 |
Table Row 2, Table Data 1 | Table Row 2, Table Data 2 |
Table Row 3 (id of trid3), Table Data 1 | Table Row 3 (id of trid3), Table Data 2 |
Table Row 1, Table Data 1 | Table Row 1, Table Data 2 |
Table Row 2, Table Data 1 | Table Row 2, Table Data 2 |
$(function(){
$('#btn5').one('click', function() {
$('<td>Appended Text</td>').appendTo('.testtable2 tr');
});
$('#btn6').one('click', function() {
tableRow1 = document.getElementById('trid1');
$(tableRow1).appendTo('.testtable2');
});
$('#btn7').one('click', function() {
tableRow3 = document.getElementById('trid3');
$(tableRow3).appendTo('table');
});
});
Related Tutorials
jQuery Intermediate Tutorials - Lesson 2 - DOM Insertion, Inside