jQuery Tutorial
.appendTo(target)
here target type can be either selector, html string, Element, Array or jQuery. The matched set of elements will be inserted at the end of the element(s) specified by this parameter.
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $("<div>Test Section</div>").appendTo('.codingpointer'); }); </script> </head> <body> <div> <div class="codingpointer">Hello</div> <div class="codingpointer">Goodbye</div> </div> </body> </html>HTML document is updated as below once appendTo is executed.
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $("<div>Test Section</div>").appendTo('.codingpointer'); }); </script> </head> <body> <div> <div class="codingpointer"> Hello <div>Test Section</div> </div> <div class="codingpointer"> Goodbye <div>Test Section</div> </div> </div> </body> </html>Output:
HelloTest SectionGoodbyeTest Section
.append() and .appendTo() methods perform the same task. Difference is in the syntax-specifically, in the placement of the content and target.
.append() method, the selector expression is preceding the container into which the content is inserted but .appendTo() method , html content precedes either as a selector expression or as markup created and it is inserted into the target container.
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $($("p")).appendTo('.codingpointer'); }); </script> </head> <body> <p>Test Section</> <div> <div class="codingpointer">Hello</div> <div class="codingpointer">Goodbye</div> </div> </body> </html>HTML document is updated as below once append is executed.
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $($("p")).appendTo('.codingpointer'); }); </script> </head> <body> <div> <div class="codingpointer"> Hello <p>Test Section</> </div> <div class="codingpointer"> Goodbye <p>Test Section</> </div> </div> </body> </html>Output:
HelloTest Section
GoodbyeTest Section
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $(' codingpointer').appendTo(".codingpointer"); }); </script> </head> <body> <div> <div class="codingpointer">Hello</div> <div class="codingpointer">Goodbye</div> </div> </body> </html>HTML document is updated as below once append is executed.
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $(' codingpointer').appendTo(".codingpointer"); }); </script> </head> <body> <div> <div class="codingpointer">Hello codingpointer</div> <div class="codingpointer">Goodbye codingpointer</div> </div> </body> </html>Output:
Hello codingpointerGoodbye codingpointer
jQuery Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page