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 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()

    $("<span>Hello! Welcome!! </span>").prependTo("p");
<p>To UsejQuery!!!</p>

Leave a Reply

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