jQuery Add New Elements, Attributes, Contents
It's simple to add new elements/content using jQuery.
Add New HTML Content
We will examine four ways used to add additional contents: jQuery
append()
- Inserts content at the end of the components that have been chosen.prepend()
- Adds content at the start of the items selected.after()
- After the specified components, material is added.before()
- Inserts contents before the items selected.
jQuery append() and prepend() Methods
The jQuery append()
function adds content to the END of the specified HTML elements you've chosen.
The jQuery prepend()
function of jQuery adds content AT THE BEGINNING of the specified HTML elements you've chosen.
Example :-
This is a paragraph.
- List item 1
- List item 2
- List item 3
Output :-
This is a paragraph.
- List item 1
- List item 2
- List item 3
Related Links
jQuery after() and before() Methods
The after()
function is used to inserts information AFTER the chosen HTML elements.
The before()
function is used to adds information BEFORE the specified HTML elements.
Example :-
Output :-

Add Several New Elements With append(), prepend(), after(), and before()
In the instances above, at the start/end of the specified HTML elements just a few text/HTML were added.
The append()
, prepend()
, after()
, and
before()
functions, on the other hand, can take an unlimited amount of additional elemnts as arguments.
Text/HTML (like in the previous instances), jQuery, or JavaScript code and DOM components can all be used to produce new elements.
Example :-
We build numerous new components in the following example. Elements are produced via text/HTML, jQuery and DOM.
Then with the function append()
we add the additional elements to the document body.
var txt1 = "Text.
"; // Create text with HTML
var txt2 = $("").text("Text."); // Create text with jQuery
var txt3 = document.createElement("p");
txt3.innerHTML = "Text."; // Create text with DOM
$("body").append(txt1, txt2, txt3); // Append new elements
Note:- This also works for other functions like prepend()
, after()
and
before()
.
Related Links