jQuery after()

jQuery after() method is used to insert the specified content after each element in the set of matched elements.

jQuery after() inserts content after the selected elements.

Syntax:

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

Content:

  • Content is a compulsory parameter of jQuery after() method, as it specifies the content to insert after the selected element.
  • It can accept the following values: HTML elements, jQuery objects, and DOM elements.

Function:

  • It is an optional parameter.
  • The function parameter is used to return the inserted content.

Index:

  • The index is an argument passed within the function.
  • It is used to give an index position to an element in the set.

Example:

<!DOCTYPE html>
<html>
<!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>How are you?</p>");
});
});
</script>
</head>
<body>
<button>Click me</button>
<p>Hello!</p>
</body>
</html>