In jQuery, both prepend() and prependTo() inbuild methods are  doing the same task, which add html elements or some content before the content of the selected elements. Then, you may think what is the difference between it. The major difference is the syntax. Let’s see the syntax for both functions:

Syntax

Syntax for prepend()

$(‘selector’).prepend(‘new text’);

This function has 2 parameters, new text is to specify the content to be inserted and the selector is to specify the elements to prepend the content.  

Syntax for prependTo()

$(‘new text’).prependTo(‘selector’);

Example for prependTo()

<script>
$(document).ready(function(){
  $("button").click(function(){
    $("<span>Hello! Welcome!! </span>").prependTo("p");
  });
});
</script>
</head>
<body>
<button>Insert</button>
<p>To UsejQuery!!!</p>
</body>

Leave a Reply

Your email address will not be published. Required fields are marked *