Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

jQuery append()


The jQuery append() method is used to insert specified content as the last child (at the end of) the selected elements in the jQuery collection.

The append () and appendTo () methods are used to perform the same task. The only difference between them is in the syntax.

Syntax:

  1. $(selector).append(content, function(index, html))

Parameters of jQuery append() method

Parameter Description
Content It is a mandatory parameter. It specifies the content which you want to insert. Its possible values are:

  • HTML elements
  • jQuery objects
  • DOM elements
Function (index,html) It is an optional parameter. It specifies the function that returns the content to insert.

  • Index: It returns the index position of the element in the set.
  • HTML: It returns the current HTML of the selected element.

Example of jQuery append() method

Let’s take an example to demonstrate the jQuery append() 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”).append(” <b>Newly added appended text</b>.”);
  9.     });
  10.     $(“#btn2”).click(function(){
  11.         $(“ol”).append(“<li><b>Newly added appended item</b></li>“);
  12.     });
  13. });
  14. </script>
  15. </head>
  16. <body>
  17. <p>This is a paragraph.</p>
  18. <p>This is another 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”>Append text</button>
  25. <button id=“btn2”>Append item</button>
  26. </body>
  27. </html>

Comments

comments