It is very easy to add new content. Add New HTML Content
There have four jQuery methods that are used to add new content:- Which are
append() – It’s Insert Content after at the end of the selected elements
prepend() - It’s Insert Content after at the beginning of the selected elements
after() - It’s Insert Content after the selected elements
before() - Inserts content before the selected elements
What is append() Method
The jQuery append() method inserts content at the end of the selected HTML elements
Example:
What is prepend() Method
The jQuery prepend() method inserts content at the beginning of the selected HTML elements.
Example
Add Several New Elements With append() and prepend()
In both examples, we have only inserted some text/HTML at the beginning/end of the selected HTML elements.
However, both the append() and prepend() methods can take an infinite number of new elements as parameters.
The new elements can be generated with text/HTML (like we have done in the examples above), with jQuery.
In following example, we create several new elements. The elements are created with text/HTML, jQuery. Then we append the new elements to the text with the append() method
Example
Text.
"; // Create element with HTML").text("Text."); // Create with jQuery
jQuery after() and before() Methods
The jQuery after() method inserts content AFTER the selected HTML elements.
The jQuery before() method inserts content BEFORE the selected HTML elements.
Example
Add Several New Elements With after() and before()
Also, both the after() and before() methods can take an infinite number of new elements as parameters. The new elements can be generated with text/HTML (like we have done in the example above), with jQuery, or with JavaScript code and DOM elements.
In the following example, we create several new elements. The elements are created with text/HTML, jQuery, and JavaScript/DOM. Then we insert the new elements to the text with the after() method (this would have worked for before() too) :
Example
Required fields are marked *
Get all latest content delivered to your email free.