insertBefore() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:insertBefore


The insertBefore() method inserts HTML elements before the selected elements.

For an existing element, it will be moved from its current position, and inserted before the selected elements.


Parameter Require Description
content Required. HTML tags content to insert.
selector Required. where to insert the content

The following code shows how to Insert a <span> element before each <p> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<script src=""></script>
        $("<span>Hello world!</span>").insertBefore("p");
    });/*from www.  j av a2 s . c  o  m*/

<button>Insert span element before each p element</button>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>


Related Tutorials