The jQuery insertAfter() method can be used to insert the content after the selected content. You have to specify the selected element as the parameter of the method.
Syntax
The syntax of this method is given below:-
Description of the Parameters
The description of these parameters is given below.
Parameter Name | Description |
---|---|
selector | Specify the element to select and add content after it. You have to use tag name, class name, or id of the elements to select. It is a required field. |
content | Specify the content to insert after the selected element. It is a required field. |
jQuery insertAfter() Method to Insert Content
If you want to insert content after the selected element, you have to use the example as given below. The content can be specified first and after that, you have to use the method and pass element as a parameter to select.
Example
1 2 3 4 5 6 7 8 9 |
<script> $(document).ready(function(){ $('.mybtn').click(function(){ $("<b>This is the bold content. </b>").insertAfter("p"); }); }); </script> <p>This is my paragraph.</p><br> <button type="button" class="mybtn">Click to Insert Content</button> |
Output
This is my paragraph.
The above example contains the paragraph text and the button element. You have to click the button given above to insert content b element after the paragraph.
You may also like to read
I hope you like this tutorial on jQuery insertAfter() method. If you have any queries regarding the tutorial, please comment below.
References