HI WELCOME TO KANSIRIS

jQuery Append – The Complete Guide with Lots of Examples

The jQuery Append method appends the content inside of every matched element. It is very helpful in doing DOM Manipulation.

Syntax

1
$(selector).append( content [, content ] [, content ] [, content ]..... )
ParameterDescription
ContentRequired. Specifies the content to insert
    Possible values:
  • HTML elements
  • jQuery objects
  • DOM elements
  • Text
[, content ]Optional. Specifies the content to insert
    Possible values:
  • HTML elements
  • jQuery objects
  • DOM elements
  • Text
You can use any number of content to add in a single append statement.
Don’t forget to check the jQuery Prepend method that is closely realted to this method.
Let us add content to a div having id myDiv.
1
<div id="myDiv">Welcome</div>
Adding a Single Content with jQuery Append
Welcome
1
$("#myDiv").append(" to YogiHosting")
It will become:
1
<div id="myDiv">Welcome to YogiHosting.</div>
Adding 3 Contents with jQuery Append
Welcome
1
$("#myDiv").append(" to YogiHosting."," Are you enjoying", " coding?")
It will become:
1
<div id="myDiv">Welcome to YogiHosting. Are you enjoying coding?</div>

jQuery Append HTML

Now I will show how to append an HTML element to a div.
1
2
3
<div id="myDiv">
    Welecome
</div>
To the above div element append a paragraph element using jQuery Append.
1
$("#myDiv").append("<p>to YogiHosting. Are you enjoying coding?</p>")
It will become.
1
2
3
4
<div id="myDiv">
    Welecome
    <p>to YogiHosting. Are you enjoying coding?</p>
</div>

jQuery Append to DOM

Using jQuery Append method, an element in the DOM can be shifted from one location to another.
Consider the following HTML:
1
2
3
4
5
6
<h4>USA</h4>
<div class="fullName">
    <p>United</p>
    <p>States</p>
    <p>America</p>
</div>
I can move the h4 element inside of div having class as fullName:
1
$(".fullName").append($("h4"));
This will make the HTML as:
1
2
3
4
5
6
<div class="fullName">
    <p>United</p>
    <p>States</p>
    <p>America</p>
    <h4>USA</h4>
</div>

jQuery Append Text

You can also append any text using the jQuery Append method. Let us append a text to the HTML paragraph:
1
2
<p>The President of United States is: </p>
$("p").append("Donald Trump");
The above paragraph will become:
1
<p>The President of United States is:  Donald Trump</p>