jQuery DOM insertion - appendTo method

appendTo method

appendTo() method is used to insert every element in the set of matched elements to the end of the target.
.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.

Insert HTML Content Inside Another HTML Element

here appends html content inside of each html element having class 'codingpointer'.
<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:
     
Hello
Test Section
Goodbye
Test Section

differences appendTo and append methods

.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.

Move HTML Element Inside Another HTML Element

here selects the html element 'p' and appends html element 'p' inside of each html element having class 'codingpointer'.
<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:
     
Hello

Test Section

Goodbye

Test Section

Insert Text Inside Another HTML Element

here appends text inside of each html element having class 'codingpointer'.
<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 codingpointer
Goodbye codingpointer



Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^