• 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 insertBefore – Insert Element Before Another Element

jQuery insertBefore – Insert Element Before Another Element

January 15, 2022 Leave a Comment

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

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

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 3 before paragraph 1, then we can use the jQuery insertBefore() method to do this with the following Javascript code.

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

The result would be as follows:

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

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

jQuery(".p1").insertBefore(".p3");

Using the jQuery insertBefore() Method to insert HTML before a DIV

We can also use the insertBefore() Method to insert new HTML before another element. Take the following HTML:

<div id="div1">
  <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>

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

$('<p class="p1">This is paragraph 1</p>').insertBefore(".p2");

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 insertBefore() method works.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Capitalize the First Letter of a String
  • 2.  Using jQuery to Get Text Length
  • 3.  How to Check if Element Exists Using jQuery
  • 4.  jQuery contains – How to Check if a Paragraph Contains Specific Text
  • 5.  Using jQuery to Disable a Button
  • 6.  jQuery mouseup – An Interactive Example of the jQuery mouseup Method
  • 7.  Using jQuery to Add id to div
  • 8.  Using jQuery to Swap an Image on Hover
  • 9.  jQuery Random Number Generator Between Two Numbers
  • 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