Step 1:Create index.html file and implement below code.
<p>This is a paragraph.</p> <p>This is another paragraph.</p> <ol> <li>List item 1</li> <li>List item 2</li> <li>List item 3</li> </ol> <button class="btn-primary" id="btn1">Prepend text</button> <button class="btn-primary" id="btn2">Prepend list item</button>
<script> $(document).ready(function(){ $("#btn1").click(function(){ $("p").prepend("<b>Prepended text</b>. "); }); $("#btn2").click(function(){ $("ol").prepend("<li>Prepended item</li>"); }); }); </script>
Complete Code For Prepend Method In JQuery
<!DOCTYPE html> <html> <head> <title>How Can I Use Prepend Method In JQuery With Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <style> body { background: darkolivegreen; } </style> <body> <div class="container"> <br><br><br> <div class="text-center"> <h2 id="color" style="color: White"> Prepend Method In JQuery</h2> </div> <br> <br> <div class="well"> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <ol> <li>List item 1</li> <li>List item 2</li> <li>List item 3</li> </ol> <button class="btn-primary" id="btn1">Prepend text</button> <button class="btn-primary" id="btn2">Prepend list item</button> </div> </div> </body> </html> <script> $(document).ready(function(){ $("#btn1").click(function(){ $("p").prepend("<b>Prepended text</b>. "); }); $("#btn2").click(function(){ $("ol").prepend("<li>Prepended item</li>"); }); }); </script>