jQuery Document Manipulation - before() Method

Example

Insert content before each p element:

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

Try It Yourself

Definition and Usage

The before() method inserts the specified content before the selected element.

Syntax

$(selector).before(content)
Parameter Description
content Required. Specifies the content to be inserted (can include HTML tags).

Use a function to insert content

Use a function to insert content before the specified element.

Syntax

$$(selector).before(function(index))

Try It Yourself

Parameter Description
function(index)

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

  • index - Optional. Accepts the index position of the selector.