jQuery | prependTo() with Examples
The prependTo() method is an inbuilt method in jQuery which is used to insert HTML elements or some content at the beginning of the selected element.
Syntax:
$(content).prepend(selector)
Parameters: This function accepts two parameters as mentioned above and described below:
- content: It is required parameter and used to specify the content to be inserted.
- selector: It is required parameter and used to specify the elements to prepend the content.
Return Value: This method returns the selected element with the specific changes made by prependTo method.
Below example illustrate the prependTo() method in jQuery:
Example:
<!DOCTYPE html> <html> <head> <title>The prependTo Method</title> <script src= </script> <!-- jQuery code to show the woirking of this method --> <script> $(document).ready(function(){ $("button").click(function(){ $("<span>Welcome to </span>").prependTo("p"); }); }); </script> <style> div{ width: 350px; min-height: 180px; font-weight: bold; padding:20px; font-size: 25px; border: 2px solid green; } </style> </head> <body> <div> <p>GeeksforGeeks!</p> <!-- Click on this button to see the change --> <button >Click Here!</button> </div> </body> </html> |
Output:

Related Articles:
Recommended Posts:
- jQuery | one() with Examples
- jQuery | first() with Examples
- jQuery | has() with Examples
- jQuery | eq() with Examples
- jQuery | last() with Examples
- jQuery | after() with Examples
- jQuery | on() with Examples
- jQuery | val() with Examples
- jQuery | unwrap() with Examples
- jQuery | closest() with Examples
- jQuery | bind() with Examples
- jQuery | clone() with Examples
- jQuery | appendTo() with Examples
- jQuery | dequeue() with Examples
- jQuery | prop() with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



