.prepend()
S2C Home « Manipulation « .prepend()
DOM insertion inside.
Description
The .prepend()
method is used to insert parameter specified content, to the beginning of each element in the matched set.
- The
.prepend()
method inserts the specified content as the 'first child' of each element in the matched set. - Use the
.append()
method to insert the specified content as the 'last child' of each element in the matched set. - The
.prepend()
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
.prepend()
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
.prepend()
and.prependTo()
methods perform the same tasks but using different syntax:- When using the
.prepend()
method the selector expression preceding the method is the container into which the content is inserted. - When using the
.prependTo()
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
Syntax
Signature | Description |
---|---|
.prepend( content [, moreContent] ) | Insert a DOM element, HTML string, or jQuery object to insert at the beginning of each element in the matched set. |
.prepend( function(index, html) ) | A function that returns a DOM element(s), HTML string, or jQuery object to insert at the beginning of each element in the matched set. |
Parameters
Parameter | Description | Type |
---|---|---|
content | A DOM element, HTML string, or jQuery object to insert at the beginning of each element in the matched set. | Element ,HTMLstring orjQuery |
moreContent | One or more additional DOM elements, arrays of elements, HTML strings, or jQuery objects to insert at the beginning of each element in the matched set. | Element ,Array ,HTMLstring or jQuery |
function(index, html) | A function.
|
Function |
Return
A jQuery
object.
.prepend( content [, moreContent] )
ExamplesTop
A DOM element, HTML string, or jQuery object to insert at the beginning of each element in the matched set.
- The
.prepend( content [, moreContent] )
signature will accept any number of additional parameters.
In the example below when the left button is pressed the first time, we prepend some text to the 'td' elements within the table with a class of 'testtable'.
When the centre button is pressed the first time, we first create two variables of the table with a class of 'testtable', table rows 1 and 2 DOM elements. We then select a single target to prepend these rows to. As stated above in the description when only a single target exists and we are prepending existing content, the elements are actually moved and not cloned to the target element. The two table rows are moved from the first to second table.
When the right button is pressed the first time, we first create a variable of the table with a class of 'testtable' table row 3 DOM element. We then select multiple targets to prepend this row to. As stated above in the description when multiple targets exist and we are prepending existing content, the elements are cloned to the target elements. The table row is prepended to the top 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 (id of trid2), Table Data 1 | Table Row 2 (id of trid2), 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(){
$('#btn8').one('click', function() {
$('.testtable td').prepend('Prepending Some Text!! ');
});
$('#btn9').one('click', function() {
tableRow1 = document.getElementById('trid1');
tableRow2 = document.getElementById('trid2');
$('.testtable2').prepend(tableRow1, tableRow2);
});
$('#btn10').one('click', function() {
tableRow3 = document.getElementById('trid3');
$('table').prepend(tableRow3);
});
});
.prepend( function(index, html) )
ExampleTop
A function that returns a DOM element(s), HTML string, or jQuery object to insert at the beginning of each element in the matched set.
In the example below when you press the button the first time, we prepend some text to each 'td' element within the table with a class of 'testtable3'.
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(){
$('#btn11').one('click', function() {
$('.testtable3 td').prepend( function(index, html) {
return 'Prepending Text:' + index + ' ';
});
});
});
Related Tutorials
jQuery Intermediate Tutorials - Lesson 2 - DOM Insertion, Inside