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

jQuery before() Method

jQuery HTML/CSS Methods

the before() method to insert specified content before the selected element.

To insert content after the selected element, please useafter()Method.

Syntax:

Inserting content:

$("selector").before(content)

Inserting content using a function:

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

Instance

Inserting content before each paragraph:

$("button").click(function(){
  $("p").before("<p>Hello world</p>");
});
Test see‹/›

Inserting content using a function:

$("button").click(function(){
  $("p").before(function(i){
    return "<p>This p's index: " + i + ".</p>";
  });
});
Test see‹/›

Parameter Value

ParameterDescription
contentSpecify the content to be inserted before each selected element (can include HTML tags)

Possible values:

  • HTML Element

  • DOM Element

  • jQuery Object

function(index)Specify a function that returns the content to be inserted
  • index-Return the index position of the element in the collection

jQuery HTML/CSS Methods