jQuery Document Operation - prepend() Method

Example

Insert content at the beginning of the p element:

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

Try it yourself

Definition and Usage

The prepend() method inserts the specified content at the beginning of the selected element (still located internally).

Tip:prepend() and prependTo() The method has the same effect. The difference lies in the syntax: the position of content and selector, and prependTo() cannot use a function to insert content.

Syntax

$().prepend(', '),content)
Parameters Description
content Required. Specifies the content to be inserted (can include HTML tags).

Use the function to append content

Use the function to insert the specified content at the beginning of the selected element.

Syntax

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

Try it yourself

Parameters Description
function(index,html)

Required. Specifies the function that returns the content to be inserted.

  • index - optional. Accepts the index position of the selector.
  • html - optional. Accepts the current HTML of the selector.