insertAfter() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:insertAfter

Description

The insertAfter() method inserts HTML elements after the selected elements.

For an existing element, it will be moved from its current position, and inserted after the selected elements.

Syntax

Parameter RequireDescription
content Required. HTML tags to insert.
selector Required. where to insert the content

The following code shows how to Insert a <span> element after each <p> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("<span>new!</span>").insertAfter("p");
    });/* ww  w.jav  a 2  s  .  c o  m*/
});
</script>
</head>
<body>

<button>Test</button>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>

Related Tutorials