Open In App

jQuery insertAfter() Method

Last Updated : 18 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
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=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </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=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </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: 


Next Article
Practice Tags :

Similar Reads