jQuery before()

jQuery before() method is used to insert the specified content before each element in the set of matched elements.

jQuery before() inserts content before the selected elements.

Syntax:

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

Content:

  • Content is a compulsory parameter of the jQuery before() method, as it specifies the content to insert before the selected element.
  • It can accept the following values: HTML elements, jQuery objects, and DOM elements.

Function:

  • It is an optional parameter.
  • The function parameter is used to return the inserted content.

Index:

  • The index is an argument passed within the function.
  • It is used to give an index position to an element in the set.

Example: