• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / jQuery appendTo – Insert Element As Last Child

jQuery appendTo – Insert Element As Last Child

December 17, 2021 Leave a Comment

We can use the jQuery appendTo method to move an element after all other children elements of their parent.

$("#div1").appendTo("#div2");

Let’s say I have the following HTML:

<div id="div1">
  <p class="p1">This is paragraph 1</p>
  <p class="p2">This is paragraph 2</p>
  <p class="p3">This is paragraph 3</p>
</div>

If we want to move paragraph 1 after paragraph 3, then we can use the jQuery appendTo() method to do this with the following Javascript code.

$(".p1").appendTo("#div1");

The result would be as follows:

This is paragraph 2
This is paragraph 3
This is paragraph 1

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery(".p1").appendTo("#div1");

The appendTo method can be less useful than say the insertAfter() method as it only will bring the selected element to the end of the parent element’s children, while the insertAfter() method can be used to move the selected element after any other element on the page.

The append method is also very similar to the jQuery append() method, it’s main difference just being a syntax one. But both can accomplish the same thing.

Using the appendTo() Method to insert HTML into a DIV

We can also use the appendTo() Method to insert HTML into the end of a div. Take the following HTML:

<div id="div1">
  <p class="p1">This is paragraph 1</p>
  <p class="p2">This is paragraph 2</p>
  <p class="p3">This is paragraph 3</p>
</div>

Say we wanted to add a new paragraph to #div1, right below class “p3”. We can use the appendTo() method to do this.

$('<p class="p4">This is paragraph 4</p>').appendTo("#div1");

The result would be as follows:

This is paragraph 1
This is paragraph 2
This is paragraph 3
This is paragraph 4

Using the appendTo() Method to Move An Element After Another With a Click

We can use the jQuery appendTo() method to move an element after all other elements of their parent element.

Let’s say we have the following HTML code and we want to give the user the ability to move paragraph 2 to the bottom.

<div id="div1">
  <p class="p1">This is paragraph 1</p>
  <p class="p2">This is paragraph 2</p>
  <p class="p3">This is paragraph 3</p>
  <p class="p4">This is paragraph 4</p>
</div>
<div id="click-me">Move Paragraph 2</div>

We can utilize both the jQuery click() method and jQuery appendTo() method to move paragraph 2 to the bottom.

Below is the Javascript code which will allow the user to be able to move paragraph 2 after all the other paragraphs of #div1.

$("#click-me").click(function(){
    $(".p2").appendTo("#div1"); // results in paragraph 2 being moved after all other elements of #div1
}); 

The final code and output for this example of how to move an element after another using the jQuery appendTo() method and Javascript is below:

Code Output:

This is paragraph 1

This is paragraph 2

This is paragraph 3

This is paragraph 4

Move Paragraph 2

Full Code:

<div id="div1">
  <p class="p1">This is paragraph 1</p>
  <p class="p2">This is paragraph 2</p>
  <p class="p3">This is paragraph 3</p>
  <p class="p4">This is paragraph 4</p>
</div>
<div id="click-me">Move Paragraph 2</div>
</div>

<script>

$("#click-me").click(function(){
  $(".p2").appendTo("#div1"); // results in paragraph 2 being moved after all other elements of #div1
});

</script>

Hopefully this article has been useful for you to understand how the jQuery appendTo() method works.

Other Articles You'll Also Like:

  • 1.  jQuery hover – An Interactive Example of the hover Method
  • 2.  Using the jQuery fadeTo Method to Change the Opacity of an Element
  • 3.  Using jQuery to Add Class to HTML Element
  • 4.  Examples of Text Animations Using jQuery
  • 5.  Using jQuery to Change the Font Size of a Paragraph
  • 6.  How to Set All Checkbox to Checked in jQuery
  • 7.  jQuery fadeIn – Fading In Element in HTML
  • 8.  Using jQuery to Remove Attribute from HTML Element
  • 9.  jQuery delay – How to Delay the Start of a Method
  • 10.  Get nth Child with jQuery

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy