.after() method in Jquery

Description:

The jQuery after() method is used to insert content after the selected element.

Syntax

  • $(selector).after(content)
  • $(selector).after(content,function(index))
Content It specifies the content which need to append.
Function(index) It specifies the function that returns the content which used to insert.

index: Get the index position of the element in the set

Passing Function:

.after() supports passing a function that returns the elements to insert like as below. It can accept any number of additional arguments.

 $("#divcontainer").after(function () {
     return "<div>Welcome to Dotnet-helpers.com</div>";
  });

Example: 

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
 <style>
 .container {
 border: 5px solid #1a80b6;
 padding: 10px;
 display: inline-block; 
 }
 </style>
 <script type="text/javascript">
 $(document).ready(function () {
 $('.btnClass').click(function () {
 $("#divcontainer").after('<div style="color:#ff0000"> This is Newly added text');
 })
 });
 </script>
</head>
<body>
 <button id="addMethod">Click to Add content as First Child</button><br /><br />
 <div id="divcontainer" class="container">
 <div>
 This is Default text
 </div>
 </div>
</body>
</html>

Output: 

By clicking the button, “this is Newly added text” will append after the selected div (divcontainer).

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.