Open In App

jQuery insertAfter() Method

Last Updated : 18 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

jQuery insertAfter() method is used to insert some HTML content after a specified element. The HTML content will be inserted after each occurrence of the specified element. 

Syntax:

$('content_to_be_inserted').insertAfter(target)

Parameters:

It takes a parameter “target” which specifies that after this target element, the passed content needs to be inserted in the HTML document.

Return Value:

It doesn’t return any value.

Example 1: In this example, the content will be added after the <p> tag by clicking the div element.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
    <script>
        $(document).ready(function () {
            $("div").click(function () {
                // insertAfter
                $("<p>You should follow GeeksForGeeks</p>").insertAfter("p");
            });
        });
    </script>
</head>
 
<body>
    <p>
        To learn jQuery :
    </p>
    <div>
        Click here to complete
    </div>
</body>
 
</html>


Output: 

Example 2: In this example, the content will be added after the all the <p> tags by clicking the div element.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
    <script>
        $(document).ready(function () {
            $("div").click(function () {
                // insertAfter
                $("<p>You should follow GeeksForGeeks</p>").insertAfter("p");
            });
        });
    </script>
</head>
 
<body>
    <p>To learn jQuery : </p>
    <p> To learn coding : </p>
    <div>Click here to complete</div>
</body>
 
</html>


Output: 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads