• 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 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.  jQuery get first child
  • 2.  Get Selected Option from Dropdown Using jQuery
  • 3.  jQuery hover – An Interactive Example of the hover Method
  • 4.  Using jQuery to Set Visibility
  • 5.  Using jQuery to Get the Position of Element
  • 6.  Using jQuery to Append Text to textarea Field
  • 7.  jQuery after – Insert HTML After Another Element
  • 8.  How to Use jQuery to Check if an Element is Visible
  • 9.  Using jQuery to Get the Parent Div of an Element
  • 10.  jQuery keyup Method

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