.after()S2C Home « Manipulation « .after()

DOM insertion outside.

Description

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

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

Syntax

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

Parameters

Parameter Description Type
contentA DOM element, HTML string, or jQuery object to insert after 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 after 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.

.after( content [, moreContent] ) ExamplesTop

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

  • The .after( 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, after 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 after the first table on the page and from there cloned after all other tables on the page.

Table1 For Testing The .after( 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 .after( 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(){
  $('#btn1').one('click', function() {
    $('.testtable tr').after('<tr><td>Adding Text!!</td><td>Adding Some Text!!</td></tr>'); 
  });
  $('#btn2').one('click', function() {
    tableRow1 = document.getElementById('trid1');
    tableRow2 = document.getElementById('trid2');
    $('#trid3').after(tableRow1, tableRow2); 
  });
  $('#btn3').one('click', function() {
    $('table').after('<p>Adding para text after each table entry!!</p>'); 
  });
});

Paragraph text with an id of 'addafter' to be added after each table.

Press the button below to action the above code:

                            


.after( function(index) ) ExampleTop

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

In the example below when you press the button the first time, we add some HTML after 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(){
  $('#btn4').one('click', function() {
    $('.testtable3 td').after( function(index, html) {
      return '<td>  Adding Text:' + index + ' </td>';
    }); 
  });
});

Press the button below to action the above code: