Home >>jQuery Tutorial >jQuery before() Method

jQuery before() Method

jQuery before() Method

jQuery before() method

Syntax:
$(selector).before(content,function(index))

Parameter Values

Parameter Description
content It is a Required parameter used to insert the specifies content (can contain HTML tags)

Possible values:

  • HTML elements
  • jQuery objects
  • DOM elements
function(index) It is used to returns the specifies content to insert
  • index – used to Returns the set of index position in the element
Here is an example of jQuery before() 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(){
$(".txt1").before("<p>Hello Phptpoint!</p>");
});
});
</script>
</head>
<body>
<button class="btn1">click to insert content</button>
<p class="txt1">PHPTPOINT</p>
<p class="txt1">This is a paragraph.</p>
</body>
</html>

Output:

PHPTPOINT

This is a paragraph.


No Sidebar ads