English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery insertBefore() method

For before() the expression is chosen before the function, with the content as a parameter

jQuery HTML/CSS Methods

The insertBefore() method inserts the specified HTML element before the selected element.

To insert an HTML element after the selected element, useinsertAfter()method.

Inbefore()and the insertBefore() method performs the same task. The main difference lies in the syntax:

  • Usebefore()The expression is chosen before the function, with the content as a parameter

  • UseinsertBefore()The content is just opposite, it will be placed in front of the element specified by the parameter

Syntax:

$(content).insertBefore(selector)

Instance

Insert HTML elements before each paragraph:

$("button").click(function(){
  $("<p style="39;color:red;'>Hello world</p>").insertBefore("p");
});
Test and see‹/›

Use the insertBefore() method to insert an existing element before each selected element:

$("button").click(function(){
  $("h1").insertBefore("p");
});
Test and see‹/›

Parameter Value

ParameterDescription
contentSpecify the content to be inserted (must contain HTML tags)

Possible values:

  • HTML Element

  • DOM Element

  • jQuery Object

selectorThe selected element will be inserted before the element specified by this parameter

jQuery HTML/CSS Methods