jQuery | after() with Examples

The after() is an inbuilt function in jQuery which is used to insert content, specified by the parameter for the each selected element in the set of matched element.
Syntax:

$(selector).after(A);

Parameter: It accepts a parameter “A” which is either a content or function passed to the method.
Return Value: It returns the selected element with the modification.

jQuery code to show the working of after() method:

Code #1:
In the below code, Element is passing to the method which will get added after the selected element.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <meta charset="utf-8">
    <style>
        p {
            background: lightgreen;
            display: block;
            width: 150px;
            padding: 10px;
            border: 2px solid green;
        }
    </style>
    </script>
</head>
  
<body>
    <p>I would like to say: </p>
    <script>
        $("p").after("<b><h2>Hello Geeks</h2></b>");
    </script>
</body>
  
</html>

chevron_right


Output:

Code #2:
In the below code, function is passed to the method that will work after the selected element.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <meta charset="utf-8">
    <style>
        p {
            background: lightgreen;
            width: 250px;
            padding: 10px;
            border: 2px solid green;
        }
    </style>
    </script>
</head>
  
<body>
    <p>This is first paragraph !!!</p>
    <p>This is the second paragraph !!! </p>
    <script>
        $("p").after(function() {
            return "<div>" + "GeeksforGeeks" + "</div>";
        });
    </script>
</body>
  
</html>

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.