.insertBefore()S2C Home « Manipulation « .insertBefore()

DOM insertion outside.

Description

The .insertBefore() method is used to insert the matched set before the specified target.

  • Use the .insertAfter() method to insert the specified content after each element in the matched set.
  • The .insertBefore() method works slightly differently when the content being added 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 moved to the first target element, and then cloned from the first target element to the other target elements.
      • Using the .insertBefore() 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 .insertBefore() and .before() method perform the same tasks but using different syntax:
    • When using the .insertBefore() method the content precedes the method, either as a selector expression or as markup created on the fly, and is inserted after the target container.
    • When using the .before() method, the selector expression preceding the method is the container after which the content is inserted.

Syntax

Signature Description
.insertBefore( target )Insert the matched set before the specified target.

Parameters

Parameter Description Type
targetA DOM element, HTML string, or jQuery object to insert the matched set before.Element,
HTMLstring or
jQuery

Return

A jQuery object.

.insertBefore( target ) ExamplesTop

A DOM element, HTML string, or jQuery object to insert the matched set before.

In the example below when the left button is pressed the first time, we add a 'tr' element with two 'td' elements in it, before the 'tr' element with an id of 'trid3'.

When the centre button is pressed the first time, we first create a variable of the table with a class of 'testtable', table rows 1 DOM element. We then select a single target to add these rows before. As stated above in the description when only a single target exists and we are adding existing content, the elements are moved and not cloned to the target element. The first two table rows in table1 are reversed.

When the right button is pressed the first time, we select the 'p' element with the id of 'addbefore'. We then select multiple targets to add this 'p' element to. As stated above in the description when multiple targets exist and we are adding existing content, the elements are moved to the first target element and then cloned from the first target element to the other target elements. So in this case the 'p' element is first moved before the first table on the page and from there cloned before all other tables on the page.

Table1 For Testing The .insertBefore( target ) 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, Table Data 1 Table Row 3, Table Data 2

Table2 For Testing The .insertBefore( target ) Signature
Table Row 1 (id of trid3), Table Data 1 Table Row 1 (id of trid3), Table Data 2


$(function(){
  $('#btn12').one('click', function() {
    $('<tr><td>Adding Text!!</td><td>Adding Some Text!!</td></tr>').insertBefore('#trid3'); 
  });
  $('#btn13').one('click', function() {
    tableRow2 = document.getElementById('trid2');
    $(tableRow2).insertBefore('#trid1'); 
  });
  $('#btn14').one('click', function() {
    aPara = document.getElementById('addbefore');
    $(aPara).insertBefore('table'); 
  });
});

Paragraph text with an id of 'addbefore' to be added before each table.

Press the button below to action the above code: