jQuery prepend()

The jQuery prepend() method is used to insert the specified content at the beginning (as a first child) of the selected elements. It is just the opposite of the jQuery append() method.

If you want to insert the content at the end of the selected elements, you should use the append method.

Syntax:

$(selector).prepend(content,function(index,html))  

Parameters of jQuery prepend() method

ParameterDescription
ContentIt is a mandatory parameter. It specifies the content which you want to insert. Its possible values are:HTML elementsjQuery objectsDOM elements
Function (index, html)It is an optional parameter. It specifies a function that returns the content which is inserted.Index:It is used to provide the index position of the element in the set.Html: : It provides the current HTML of the selected element.

Example of jQuery prepend() method

Let’s take an example to demonstrate the jQuery prepend() method.


  1. <!DOCTYPE html>    
  2. <html>    
  3. <head>    
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>    
  5. <script>    
  6. $(document).ready(function(){    
  7.     $("#btn1").click(function(){    
  8.         $("p").prepend("<b>Prepended text</b>. ");    
  9.     });    
  10. });    
  11. </script>    
  12. </head>    
  13. <body>    
  14. <p>This is the first paragraph.</p>    
  15. <p>This is the second paragraph.</p>    
  16. <button id="btn1">Prepend text</button>    
  17. </body>    
  18. </html> 

Output:

This is the first paragraph.

This is the second paragraph.Prepend text

jQuery prepend() example 2


  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.     $("#btn1").click(function(){  
  8.         $("p").prepend("<b>Prepended text</b>. ");  
  9.     });  
  10.     $("#btn2").click(function(){  
  11.         $("ol").prepend("<li>Prepended item</li>");  
  12.     });  
  13. });  
  14. </script>  
  15. </head>  
  16. <body>  
  17. <p>This is the first paragraph.</p>  
  18. <p>This is the second paragraph.</p>  
  19. <ol>  
  20.   <li>Item no.1</li>  
  21.   <li>Item no.2</li>  
  22.   <li>Item no.3</li>  
  23. </ol>  
  24. <button id="btn1">Prepend text</button>  
  25. <button id="btn2">Prepend list item</button>  
  26. </body>  
  27. </html>  

Output:

This is the first paragraph.

This is the second paragraph.

  1. Item no.1
  2. Item no.2
  3. Item no.3

Prepend text Prepend list item

jQuery prepend() example 3

<!DOCTYPE html>  

<html lang="en">  

<head>  

  <meta charset="utf-8">  

  <title>prepend demo</title>  

  <style>  

  p {  

    background: lightpink;  

  }  

  </style>  

  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  

</head>  

<body>  

 <p>javatpoint.com</p>  

<p>Guys! Welcome to the best tutorial site.</p>  

 <script>  

$( "p" ).prepend( "<b>Hello </b>" );  

</script>  

 </body>  

</html>

Output:

Hello wisdom.com

Hello Guys! Welcome to the best tutorial site.


Comments

Leave a Reply

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