• 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
  • VBA
  • About
You are here: Home / jQuery / jQuery insertAfter – Insert Element After Another Element

jQuery insertAfter – Insert Element After Another Element

January 15, 2022 Leave a Comment

We can use the jQuery insertAfter method to move an element after another element, or to insert a new element after another element.

$("#div1").insertAfter("#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 insertAfter() method to do this with the following Javascript code.

$(".p1").insertAfter(".p3");

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").insertAfter(".p3");

Using the jQuery insertAfter() Method to insert HTML after a DIV

We can also use the insertAfter() Method to insert new HTML after another element. 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 insertAfter() method to do this.

$('<p class="p4">This is paragraph 4</p>').insertAfter(".p3");

The result would be as follows:

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

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

Other Articles You'll Also Like:

  • 1.  Using jQuery to Replace a Class
  • 2.  jQuery has – Check if an Element Has Other Elements
  • 3.  jQuery mouseover – An Interactive Example of the mouseover Method
  • 4.  How to Check if Element Exists Using jQuery
  • 5.  How to Filter a List of Divs with a Text Input Bar Using Javascript
  • 6.  Changing the Background Image of a div Using jQuery
  • 7.  Using jQuery to Hide Element by Class
  • 8.  Using jQuery to Add a Sibling to an Element
  • 9.  jQuery keyup Method
  • 10.  jQuery siblings – Get the Sibling Elements of a Div

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy