
- jQuery - Home
- jQuery - Roadmap
- jQuery - Overview
- jQuery - Basics
- jQuery - Syntax
- jQuery - Selectors
- jQuery - Events
- jQuery - Attributes
- jQuery - AJAX
- jQuery CSS Manipulation
- jQuery - CSS Classes
- jQuery - Dimensions
- jQuery - CSS Properties
- jQuery Traversing
- jQuery - Traversing
- jQuery - Traversing Ancestors
- jQuery - Traversing Descendants
- jQuery References
- jQuery - Selectors
- jQuery - Events
- jQuery - Effects
- jQuery - HTML/CSS
- jQuery - Traversing
- jQuery - Miscellaneous
- jQuery - Properties
- jQuery - Utilities
- jQuery Plugins
- jQuery - Plugins
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
jQuery appendTo() Method
The appendTo() method in jQuery is used to append HTML elements at the end of the set of matched elements. This method accepts a parameter named "selector", which specifies the target element(s) to which the content will be appended at the end.
If we want to append the HTML elements at the beginning of the selected target elements, we need to use the prependTo() method.
Syntax
Following is the syntax of appendTo() method in jQuery −
$(content).appendTo(selector)
Parameters
This method accepts the following parameters −
- content: The HTML elements to be appended at the end of the target element(s).
- selector: The target element(s) to which the content will be appended.
Note: If the content provided is already an existing element, it will be moved from its current position in the DOM and then inserted at the end of chosen target element(s).
Example 1
In the following example, we are using the appendTo() method to append a <span> element at the end of the <p> element −
<html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("<span><i> This is a newly appended span element.</i></span>").appendTo("p"); }) }); </script> </head> <body> <p>Paragraph element.</p> <button>Click here!</button> </body> </html>
After executing and clicking the button, the <span> element will be appended at the end of <p> element.
Example 4
In the below example, we are appending an existing element (<span>) at the end of the paragraph element −
<html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("span").appendTo("p"); }) }); </script> </head> <body> <span><i> This is a newly appended span element.</i></span> <p>Paragrapah element.</p> <button>Click here!</button> </body> </html>
As the provided content is already an existing element, it will be moved from its current position in the DOM and then inserted after the <p> (target element).