append() and prepend() in jQuery
append() and prepend() Methods
As we know that html() and text() methods are used to get and set the content of any element. However, the main problem arises when we set the content in them. The data will be lost as we set content in elements using these methods. So, for this purpose, we have to use append() and prepend() methods.
append():-
In append() method the content will be added at the end of the current element.
Syntax:-
Its syntax is:- $(selector).append(content, function(index,html))
Further Explanation:-
Parameter | Description |
---|---|
content | Required. Specifies the content to insert. |
function(index,html) | Optional. Specifies a function that returns the content to insert
|
prepend():-
In prepend() the content will be added at the start of the given element.
Syntax:-
Its syntax is:- $(selector).prepend(content, function(index, html))
Further Explanation:-
Parameter | Description |
---|---|
content | Required. Specifies the content to insert. |
function(index,html) | Optional. Specifies a function that returns the content to insert
|