Query append() and appendTo()
append() :ย Append to end of the selected element
HTML : ย <ul id=”test”>
<li>test</li>
</ul>
JQuery : ย jQuery(“#test”).append(“<li>test1</li>”);
OUTPUT :ย <ul id=”test”>
<li>test</li>
<li>test1</li>
</ul>
appendTo :ย Append to end of the selected element
HTML :ย ย <div>Hello</div>
JQuery :ย $(‘<p>Test</p>’).appendTo(‘.inner’);
OUTPUT :ย <div ย class=”inner” > Hello
<p>Test</p>
</div>
Difference:
Both jQuery append() and appendTo() methods are doing the same task, add a text or html
content after the content of the matched elements. The major difference is in the syntax.
//Create our content
var appendHTML = “<p>I’m going to be added to the DOM</p>”;
//Select our element, then add the html
$(“#container”).append(appendHTML);
//Create our content, then select the element to append to
$(appendHTML).appentTo(“#container”);
Leave A Comment