insertAfter method in jQuery with example

1. insertAfter() method inserts every element in the set of matched elements after the target.

Example 1 :

        <script type="text/javascript" src=""> 
        <script type="text/javascript">
            $(document).ready(function () {
                $("#myButton").click(function () {
                    $("<span>This is span element.</span><br/>").insertAfter($('#targetP'));
        <p id="targetP">This is p element. On clicking the button span will be added after the p element.</p>
        <input type="button" id="myButton" value="Click ME"/>
        <input type="button" value="Reset" onclick=""location.reload()"/>
Demo :

In above example, we are using insertAfter()  method to insert span element after the target element specified as selector to insertAfter() method.


Post a Comment