jQuery after

The jQuery after() method is used to insert specified content after the selected element. It is just like jQuery append() method.

If you want to insert content before the selected element, you should use jQuery before() method.

Syntax:

$(selector).after(content,function(index))   

Parameters of jQuery after() method

ParameterDescription
ContentIt is a mandatory parameter. It specifies the content to insert. Its possible values are:HTML elementsjQuery objectsDOM elements
Function (index)It specifies a function that returns the content which is used to insert.index: It provides the index position of the element in the set.

Example of jQuery after() method

Let’s see an example of jQuery after() method:

<!DOCTYPE html>  

<html>  

<head>  

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  

<script>  

$(document).ready(function(){  

    $("button").click(function(){  

        $("p").after("<p><b>Hello javatpoint.com</b></p>");  

    });  

});  

</script>  

</head>  

<body>  

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

<p>This is a tutorial website.</p>  

<p>This is a training institute.</p>  

</body>  

</html>

Output:Insert content after each p element

This is a tutorial website.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *