before() method in jQuery

before() in jQuery

This method will insert contents specified in the parameter, before each element in the set of matched elements.

If there are more than one target element, cloned copies of the inserted element will be created for each target except for the last one.

Syntax 1

$(selector).before(content[,content])

  • content - HTML string, DOM element, text node, array of elements and text nodes, or jQuery object which is to be inserted before each element in the set of matched elements.
Syntax 2

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

  • index - Index position of the element in the set.
  • currentElement - old HTML value of the element.

Example 1 - Append the content

Try Yourself

Example 2 - Append multiple content

Try Yourself

Example 3 - Append html content

Try Yourself

Example 4 - Append content through function

Try Yourself