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

jQuery prepend() method

jQuery HTML/CSS Methods

The prepend() method inserts the specified content at the beginning of each selected element (as the first child element).

To insert content at the end of the selected element, useappend()Method.

Syntax:

Insert prefix content:

$.selector.prepend(content)

Using a function to add content:

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

Example

Add some text content before all paragraphs:

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

Add some HTML before all paragraphs:

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

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

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

Using a function to add content:

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

Parameter values

ParametersDescription
contentSpecify the content to be inserted at the beginning 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