.before()S2C Home « Manipulation « .before()

DOM insertion outside.

Description

The .before() method is used to insert parameter specified content, before each element in the matched set.

  • Use the .after() method to insert the specified content after each element in the matched set.
  • The .before() 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 .before() 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 .before() and .insertBefore() methods perform the same tasks but using different syntax:
    • When using the .before() method the selector expression preceding the method is the container before which the content is inserted.
    • 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 before the target container.

Syntax

Signature Description
.before( content [, moreContent] )Insert a DOM element, HTML string, or jQuery object before each element in the matched set.
.before( function(index) )A function that returns a DOM element(s), HTML string, or jQuery object to insert before each element in the matched set.

Parameters

Parameter Description Type
contentA DOM element, HTML string, or jQuery object to insert before 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 before each element in the matched set. Element,
Array,
HTMLstring or
jQuery
function(index)A function.
  • Each time the callback runs, it is passed the current index position of the element in the set.
  • 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.

.before( content [, moreContent] ) ExamplesTop

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

  • The .before( content [, moreContent] ) signature will accept any number of additional parameters.

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 each table row in 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 add these rows after. 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. These two table rows are moved from the first to second table.

When the right button is pressed the first time, we select the 'p' element with the id of 'addafter'. 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 to before the first table on the page and from there cloned before all other tables on the page.

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

Table2 For Testing The .before( 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
Table Row 3 (id of trid3), Table Data 1 Table Row 3 (id of trid3), Table Data 2


$(function(){
  $('#btn5').one('click', function() {
    $('.testtable tr').before('<tr><td>Adding Text!!</td><td>Adding Some Text!!</td></tr>'); 
  });
  $('#btn6').one('click', function() {
    tableRow1 = document.getElementById('trid1');
    tableRow2 = document.getElementById('trid2');
    $('#trid3').before(tableRow1, tableRow2); 
  });
  $('#btn7').one('click', function() {
    $('table').before('<p>Adding para text after each table entry!!</p>'); 
  });
});

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

Press the button below to action the above code:

                            


.before( function(index) ) ExampleTop

A function that returns a DOM element(s), HTML string, or jQuery object to add before each element in the matched set.

In the example below when you press the button below the first time, we insert some HTML before each 'td' element within the table with a class of 'testtable3'.

Table For Testing The .after( function(index) ) 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() {
    $('.testtable3 td').before( function(index, html) {
      return '<td>  Adding Text:' + index + ' </td>';
    }); 
  });
});

Press the button below to action the above code: