jQuery insertAfter() Method
Last Updated :
18 Dec, 2023
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
Share your thoughts in the comments
Please Login to comment...