To move an HTML element after another using jQuery the simplest way is to use the insertAfter() method.
$("#div-1").insertAfter("#div-2");
Let’s say I have the following HTML:
This is paragraph 1
This is paragraph 2
This is paragraph 3
If we want to move paragraph 1 after paragraph 3, then we can use the jQuery insertAfter() method to do this with the following Javascript code.
$(".p1").insertAfter(".p3");
If you are using WordPress, don’t forget to change the $ to jQuery as below:
jQuery(".p1").insertAfter(".p3");
If you’d instead like to move an element before another using jQuery, you can use the jQuery insertBefore() method.
Moving an Element After Another Using jQuery With a Click
Many times when creating a web page and the user experience, we want to move a particular element based on an interaction with another element on the web page.
We can move an element after another element using jQuery very easily by combining the insertAfter() method with a click event.
Let’s say we have the following HTML code and we want to give the user the ability to move paragraph 1 after paragraph 2.
Click Me to Move Paragraph 1 After Paragraph 2
This is paragraph 1
This is paragraph 2
This is paragraph 3
We can utilize both the jQuery click() method and jQuery insertAfter() method to move paragraph 1 after paragraph 2.
Below is the Javascript code which will allow the user to be able to move paragraph 1 after paragraph 2:
$("#click-me").click(function(){
$(".p1").insertAfter(".p2"); // results in paragraph 1 being moved after paragraph 2
});
The final code and output for this example of how to move an element after another using jQuery and Javascript is below:
Code Output:
Click Me to Move Paragraph 1 after Paragraph 2
This is paragraph 1
This is paragraph 2
This is paragraph 3
Full Code:
Click Me to Move Paragraph 1 after Paragraph 2
This is paragraph 1
This is paragraph 2
This is paragraph 3
<script>
$("#click-me").click(function(){
$(".p1").insertAfter(".p2"); // results in paragraph 1 being moved after paragraph 2
});
</script>
Using the appendTo() Method to Move An Element After Another
The jQuery appendTo() method is useful when it comes to manipulating web pages.
We can use the jQuery appendTo() method to move an element after all other elements in a parent element.
Let’s say we have the following HTML code and we want to give the user the ability to move paragraph 1 to the bottom.
Click Me to Move Paragraph 1 after all the other elements
This is paragraph 1
This is paragraph 2
This is paragraph 3
We can utilize both the jQuery click() method and jQuery appendTo() method to move paragraph 1 to the bottom.
Below is the Javascript code which will allow the user to be able to move paragraph 1 after all the other paragraphs in #div1.
$("#click-me").click(function(){
$(".p1").appendTo("#div1"); // results in paragraph 1 being moved after all other elements in #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:
Click Me to Move Paragraph 1 after all the other elements
This is paragraph 1
This is paragraph 2
This is paragraph 3
Full Code:
Click Me to Move Paragraph 1 after all the other elements
This is paragraph 1
This is paragraph 2
This is paragraph 3
<script>
$("#click-me").click(function(){
$(".p1").appendTo("#div1"); // results in paragraph 1 being moved after all other elements in #div1
});
</script>
Hopefully this article has been useful for you to understand how to move an element after another using jQuery.