Insert content after each <p> element:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>//ww w . ja va 2 s . c o m <script> $(document).ready(function(){ $("button").click(function(){ $("p").after("<p>Hello world!</p>"); }); }); </script> </head> <body> <button>Insert content after each p element</button> <p>This is a paragraph.</p> <p>This is another paragraph.</p> </body> </html>
The after()
method inserts content after the selected elements.
$(selector).after(content,function(index))
Parameter | Optional | Description |
---|---|---|
content | Required. | the content to insert Possible values: HTML elements jQuery objects DOM elements |
function(index) | Optional | sets a function that returns the content to insert index - the index position of the element in the set |