• 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 / Using jQuery prepend to Insert Element As First Child

Using jQuery prepend to Insert Element As First Child

December 14, 2021 Leave a Comment

We can use the jQuery prepend() method to move an element before others. It works in a very similar way as the jQuery prependTo() Method.

$("#div1").prepend($(".p3"));

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 prepend() method to do this with the following Javascript code.

$("#div1").prepend($(".p3"));

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("#div1").prepend(".p3");

The prepend method can be less useful than say the insertBefore() method as it only will bring the selected element to the beginning of the parent element, while the insertBefore() method can be used to move the selected element before any other element in the page.

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

We can also use the prepend() Method to insert HTML into the start of a div. 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 above class “p2”. We can use the prepend() method to do this.

$('#div1').prepend('<p class="p1">This is paragraph 1</p>');

The result would be as follows:

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

Using the prepend() Method to Move An Element Before Another With a Click

We can use the jQuery prepend() method to move an element before 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 3 to the top.

<p id="click-me">Click Me to Move Paragraph 3 before all the other elements</p>
<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>

We can utilize both the jQuery click() method and jQuery prepend() method to move paragraph 3 to the top.

Below is the Javascript code which will allow the user to be able to move paragraph 3 before all the other paragraphs in #div1.

$("#click-me").click(function(){
    $("#div1").prepend(".p3"); // results in paragraph 3 being moved before all other elements in #div1
}); 

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

Code Output:

Click Me to Move Paragraph 3 before all the other elements below.

This is paragraph 1

This is paragraph 2

This is paragraph 3

This is paragraph 4

Full Code:

<div class="html-code-output"><p id="click-me">Click Me to Move Paragraph 3 before all the other elements below.</p>
<div id="div1-1">
  <p class="p1-1">This is paragraph 1</p>
  <p class="p2-1">This is paragraph 2</p>
  <p class="p3-1">This is paragraph 3</p>
  <p class="p4-1">This is paragraph 4</p>
</div>
</div>

<script>

$("#click-me").click(function(){
    $("#div1-1").prepend($(".p3-1")); // results in paragraph 3 being moved before all other elements in #div1
});

</script>

Hopefully this article has been useful for you to understand how to move an element before another using jQuery.

Other Articles You'll Also Like:

  • 1.  jQuery get first child
  • 2.  Using jQuery to Change the Text Color of a Paragraph
  • 3.  jQuery Get Last Child
  • 4.  jQuery hover – An Interactive Example of the hover Method
  • 5.  Using jQuery to Convert a String to Uppercase
  • 6.  How to Filter a List of Divs with a Text Input Bar Using Javascript
  • 7.  Using jQuery to Get the Current URL
  • 8.  Using jQuery to see if URL Contains Specific Text
  • 9.  Using jQuery to Set Select to the First Option
  • 10.  jQuery mouseleave – An Interactive Example of the mouseleave 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

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