Home >>jQuery Tutorial >jQuery insertBefore() Method

jQuery insertBefore() Method

jQuery insertBefore() Method

jQuery insertBefore() method in jQuery is used to inserts HTML elements before the selected elements.

Syntax:
$(content).insertBefore(selector)

Parameter Values

Parameter Description
content It is a Required parameter and is used to Specifies the content to insert
selector It is Required parameter and is used to Specifies where to insert the content
Here is an Example of jQuery insertBefore() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".btn1").click(function(){
$("<span>Hello Phptpoint!</span>").insertBefore(".txt1");
});
});
</script>
</head>
<body>
<button class="btn1">click me to insert span element</button>
<br><br>
<p class="txt1">This is a first paragraph.</p>
<p class="txt1">This is second paragraph.</p>
</body>
</html>

Output:


This is a first paragraph.

This is second paragraph.