Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




HTML METHODS - insertAfter()


The insertAfter() method in JQuery is used to insert HTML element right after an HTML element.


Example :



<html>
    <head>
    	<title> My First Programme </title>
  	</head>

  	<body>
    	<h1> JQuery </h1>
    	<p>New Code</p>
    	
    	<button> Click Here </button>
    	

      	<script src = "https://cdnjs.cloudflare.com/ajax/libs/JQUERY/3.3.1/jquery.min.js"> </script>
	  
      	<script>

      		$('button').click( function() {
				$('<p> This is a new paragraph </p>').insertAfter('p');
			});

      	</script>      
   </body>
</html>


Output :




So, in the above code, we have to to add a new content <p> This is a new paragraph </p>, right after the <p> element,


<p>New Code</p>

And we have done it using the JQuery statement,


$('button').click( function() {
	$('<p> This is a new paragraph </p>').insertAfter('p');
});

So, on button click, the insertAfter() function is called,


$('<p> This is a new paragraph </p>').insertAfter('p');

And the new content,


<p> This is a new paragraph </p>

Is added after,


<p>New Code</p>

Just note that This is a new paragraph is a new element that is added after the <p> element.

JQuery HTML Methods insertAfter()