Home >>jQuery Tutorial >jQuery prependTo() Method

jQuery prependTo() Method

jQuery prependTo() Method

jQuery prependTo() method in jQuery is used to inserts HTML elements at the beginning of the selected elements.

Syntax:
$(content).prependTo(selector)

Parameter Values

Parameter Description
content It is a required parameter and is used to Specifies the content to insert.
selector It is a required parameter and is used to specifies on which elements to prepend the content
Here is an Example of jQuery prependTo() 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>").prependTo(".txt1");
});
});
</script>
</head>
<body>
<p class="txt1">This is first paragraph.</p>
<p class="txt1">This is second paragraph.</p>
<button class="btn1">Click me to insert span element</button>
</body>
</html>

Output:

This is first paragraph.

This is second paragraph.


No Sidebar ads