jQuery append()

The jQuery append() inserts content at the end of the selected elements.

Syntax:

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

Content:

  • Content is a compulsory parameter of jQuery append() method, as it specifies the content to insert at the end of the selected element.
  • Its can accept following values: HTML elements, jQuery objects and DOM elements.

Function:

  • It is an optional parameter.
  • The function parameter is used to return the inserted content.

Index:

  • Index is an argument passed within the function.
  • It is used to give an index position to an element in the set.

Html:

  • It provides the current HTML of the selected element.

Example 1

<!DOCTYPE html>  
<html>  
<head>  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
<script>  
$(document).ready(function(){  
$("button").click(function(){  
$("p").append("How are you? ");  
});  
});  
</script>  
</head>  
<body>  
<p>Hello! </p>  
<button>Click me</button>  
</body>  
</html>

Try it

JS Bin on jsbin.com

Example 2

<!DOCTYPE html>  
<html>  
<head>  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
<script>  
$(document).ready(function(){  
$("button").click(function(){  
 $("ol").append("<li>Brain</li>");  
});  
});  
</script>  
</head>  
<body>  
<p>Do you have?</p>  
<ol>  
  <li>Ear</li>  
  <li>Eyes</li>  
  <li>Nose</li>  
</ol>  
<button>Click me</button>  
</body>  
</html>

Try it

JS Bin on jsbin.com

Please Share