Say, you want to make any modification to the existing HTML document. And this is where the Insert methods in JQuery comes into picture.
Let us understand it with the below example, where we have a <p> element,
<p> New Code </p>
With content New Code. And we want to add a new content plus another code, and make it New Code plus another code.
And this can be achieved with the append() method.
The append() method is used to insert a content at the end of an HTML element.
<html> <head> <title> My First Programme </title> </head> <body> <h1> JQuery </h1> <p>New Code</p> <button> Click to set a new value </button> <script src = "https://cdnjs.cloudflare.com/ajax/libs/JQUERY/3.3.1/jquery.min.js"> </script> <script> $('button').click( function() { $('p').append(" plus another code"); }); </script> </body> </html>
So, in the above code, we have to to add a new content plus another code, and make it New Code plus another code
<p>New Code</p>
And we have done it using the JQuery statement,
$('button').click( function() { $('p').append(" plus another code"); });
So, on button click, the append() function is called,
$('p').append(" plus another code");
And the content of <p> element
New Code
Is added with,
plus another code
And the new content is printed on the screen,
New Code plus another code
Similarly, there are other Insert methods that helps us modify the contents of the HTML elements.
In this tutorial, we will be discussing the commonly used Insert methods i.e. append(), prepend(), after() and before(),
And you can click on the below getter methods to understand in detail :
The prepend() method is used to insert a content at the beginning of an HTML element.
<html> <head> <title> My First Programme </title> </head> <body> <h1> JQuery </h1> <p>New Code</p> <button> Click to set a new value </button> <script src = "https://cdnjs.cloudflare.com/ajax/libs/JQUERY/3.3.1/jquery.min.js"> </script> <script> $('button').click( function() { $('p').prepend("Added new content at the beginning "); }); </script> </body> </html>
So, in the above code, we have to to add a new content
'Added new content at the beginning '
At the beginning of,
<p>New Code</p>
And make it
Added new content at the beginning New Code
And we have done it using the JQuery statement,
$('button').click( function() { $('p').prepend("Added new content at the beginning "); });
So, on button click, the prepend() function is called,
$('p').prepend("Added new content at the beginning ");
And the content of <p> element,
New Code
Is added with,
Added new content at the beginning
At the beginning, and the new content is printed on the screen,
Added new content at the beginning New Code
The after() method is used to insert a content after an HTML element.
<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').after("<p> This is a new paragraph </p>"); }); </script> </body> </html>
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').after("<p> This is a new paragraph </p>"); });
So, on button click, the after() function is called,
$('p').after("<p> This is a new paragraph </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.
The before() method is used to insert a content before an HTML element.
<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').before("<p> This is a new paragraph </p>"); }); </script> </body> </html>
So, in the above code, we have to to add a new content <p> This is a new paragraph </p>, right before the <p> element,
<p>New Code</p>
And we have done it using the JQuery statement,
$('button').click( function() { $('p').before("<p> This is a new paragraph </p>"); });
So, on button click, the before() function is called,
$('p').before("<p> This is a new paragraph </p>");
And the new content,
<p> This is a new paragraph </p>
Is added before,
<p>New Code</p>
Just note that This is a new paragraph is a new element that is added before the <p> element.