English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery append() method

jQuery HTML/CSS Methods

The append() method inserts the specified content at the end of each selected element (as the last child element).

To insert content at the beginning of the selected element, useprepend()Method.

Syntax:

Insert content:

$(selector).append(content)

Insert content using a function:

$(selector).append(function(index, html))

Example

Add some text content to all paragraphs:

$("button").click(function(){
  $("p").append("Hello world");
});
Test and see‹/›

Insert HTML into all paragraphs:

$("button").click(function(){
  $("p").append("<b>Hello world</b>");/b>");
});
Test and see‹/›

This example uses document.createTextNode() to create a text node and insert it into all <p> elements:

$("button").click(function(){
  $("p").append(document.createTextNode("Hello world"));
});
Test and see‹/›

Insert content using a function:

$("button").click(function(){
  $("p").append(function(i){
       return "<b>The p element has index " + i + ".</b>";
  });
});
Test and see‹/›

Parameter value

ParameterDescription
contentSpecify the content to be inserted at the end of each selected element (can include HTML tags)

Possible values:

  • HTML Element

  • DOM Element

  • jQuery Object

function(index, html)Specify a function that returns the content to be inserted
  • index-Return the index position of the element in the collection

  • html-Return the current HTML of the selected element

jQuery HTML/CSS Methods