Example

Insert content at the beginning of each p element:

$("button").click(function(){
  $("p").prepend(" <b>Hello world!</b>");
});

Try it yourself »

Definition and Usage

The prepend() method inserts specified content at the beginning of (but still inside) the selected elements.

Tip: The prepend() and prependTo() methods do the same thing. The difference is in the syntax: the placement of the content and selector, and that prependTo() cannot insert content using a function.


Syntax

$(selector).prepend(content)

Parameter Description
content Required. Specifies the content to insert (can contain HTML tags)


Prepend Content Using a Function

Using a function to insert specified content at the beginning of the selected elements.

Syntax

$(selector).prepend(function(index,html))
Try it yourself »

Parameter Description
function(index,html) Required. Specifies a function that returns the content to insert.
  • index - Optional. Receives the index position of the selector
  • html - Optional. Receives the current HTML of the selector