.prepend() JQ 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 duplicate id attributes, which are supposed to be unique. Avoid cloning elements with the id attribute or if absolutely necessary ensure elements that might be cloned are coded using the class attribute identifier instead.
  • 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.

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
contentA DOM element, HTML string, or jQuery object to insert at the beginning of each element in the matched set.Element,
HTMLstring or
jQuery
moreContentOne 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.
  • Each time the callback runs, it is passed the current index position of the element in the set.
  • The existing value of the old HTML for the element is also passed.
  • The callback is fired in the context of the current element in the set, so the keyword this refers to that element.
Function

Return

A jQuery object.

.prepend( content [, moreContent] ) Example  Manip.  <<  Top

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.

Table1 For Testing The .prepend( content [, moreContent] ) Signature
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

Table2 For Testing The .prepend( content [, moreContent] ) Signature
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); 
  });
});

Press the button below to action the above code:

                            

.prepend( function(index, html) ) Example  Manip.  <<  Top

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 For Testing The .prepend( function(index, html) ) Signature
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 + '  ';
    }); 
  });
});

Press the button below to action the above code:

Related Tutorials

jQuery Intermediate Tutorials - Lesson 2 - DOM Insertion, Inside