The insertBefore method

The insertBefore method adds text before the given element. There is also a before method that works in a similar way.

Syntax

Insert text before an element:

javascript$(text).insertBefore(selector);

The content can be not only plain text, but also a DOM element, selector, array of elements, string or jQuery object.

Example

Let's insert text with tags before the given paragraph:

html<p id="test">paragraph indent indention indentation indenture rubric</p>
javascript$('<p>!!!</p>').insertBefore('#test');

HTML the code will look like this:

html<p>!!!</p><p id="test">paragraph indent indention indentation indenture rubric</p>

See also