The prepend() method in JQuery is used to insert a content at the beginning of an HTML element.
<html> <head> <title> My First Programme </title> </head> <body> <h1> JQuery </h1> <p>New Code</p> <button> Click to set a new value </button> <script src = "https://cdnjs.cloudflare.com/ajax/libs/JQUERY/3.3.1/jquery.min.js"> </script> <script> $('button').click( function() { $('p').prepend("Added new content at the beginning "); }); </script> </body> </html>
So, in the above code, we have to to add a new content
'Added new content at the beginning '
At the beginning of,
<p>New Code</p>
And make it
Added new content at the beginning New Code
And we have done it using the JQuery statement,
$('button').click( function() { $('p').prepend("Added new content at the beginning "); });
So, on button click, the prepend() function is called,
$('p').prepend("Added new content at the beginning ");
And the content of <p> element,
New Code
Is added with,
Added new content at the beginning
At the beginning, and the new content is printed on the screen,
Added new content at the beginning New Code