18 of 119 menu

The before method

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

Syntax

Insert text before an element:

$(selector).before(text);

This is how the text that will be returned by the custom function will be added before the selected elements:

$(selector).before(function(number in set));

Another option for using the function, the second parameter specifies the old html value of the element:

$(selector).before(function(number in set, html string));

The text can also be not only regular text, but also a DOM element or a jQuery object. In this case, these elements will move from their position in the HTML code.

Example

Let's insert some text before the given paragraph:

<p id="test">text</p> $('#test').before('!!!');

HTML the code will look like this:

!!!<p id="test">text</p>

Example

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

<p id="test">text</p> $('#test').before('<p>!!!</p>');

HTML the code will look like this:

<p>!!!</p><p id="test">text</p>

Example

Let's put one paragraph above the other (that is, cut the paragraph from the old place and put it in the new one):

<p id="p1">text1</p> <p id="p2">text2</p> $('#p2').before('#p1');

HTML the code will look like this:

<p id="p2">text2</p> <p id="p1">text1</p>

See also

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline