jQuery Document Operation - insertAfter() Method

Example

Insert a span element before each p element:

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

Try it yourself

Definition and usage

The insertBefore() method inserts HTML tags or existing elements before the selected element.

Note:If this method is used for existing elements, these elements will be moved from their current position and then added before the selected element.

Syntax

$(content).insertBefore(selector)
Parameters Description
content

Required. Specifies the content to be inserted. Possible values:

  • Selector expression
  • HTML tag
selector Required. Specifies where the selected element should be inserted.

More examples

Insert an existing element
How to use the insertAfter() method to insert an existing element before each selected element.