Add element returned from a function using after() - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:after

Description

Add element returned from a function using after()

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(){
        $("p").after(function(n){
            return "<div>index " + n + ".</div>";
        });//from  ww w .  j a va 2s  .  c  o  m
    });
});
</script>
</head>
<body>

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

<button>Insert after each p element</button>

</body>
</html>

Related Tutorials