jQuery | appendTo() with Examples

The appendTo() is an inbuilt method in jQuery that is used to insert HTML element at the end of the selected element.
Syntax:

$(content).appendTo(selector)

Here the element content specifies the content to be inserted.
Parameters: It accepts a parameters “selector” which specifies the elements to which content will be appended.
Return Value: It returns the modified selected element.

jQuery code to show the working of this method:

Code #1:
In the below code, id content with element “span” will be append to id “#hel” element.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
   <head>
      <meta charset="utf-8">
      <style>
         #hel {
         background: lightgreen;
         display: block;
         border: 2px solid green;
         padding: 10px;
         width: 300px;
         }
      </style>
      <script src="https://code.jquery.com/jquery-1.10.2.js">
      </script>
   </head>
   <body>
      <span>geeks Writer !!!</span>
      <div id="hel">Hello-  </div>
      <script>
         $("span").appendTo("#hel");
      </script>
   </body>
</html>

chevron_right


Output:

Code #2:
In the below code, we are directly appending a “p” element using this method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
   <head>
      <meta charset="utf-8">
      <style>
         body{
         display: block;
         width: 250px;
         height: 100px;
         border: 2px solid green;
         padding: 10px;
         }
      </style>
      <script src="https://code.jquery.com/jquery-1.10.2.js">
      </script>
   </head>
   <body>
      <h2>Greetings from gfg !</h2>
      <div class="container">
         <div class="inner">
            Hello
         </div>
      </div>
      <script>
         $( "<p>Everyone !!!</p>").appendTo( ".inner" );
      </script>
   </body>
</html>

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.