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

jQuery before – Insert HTML Before Another Element

April 19, 2022 Leave a Comment

We can use the jQuery before method to insert a new HTML element before another element.

$("#div2").before("<div id='div1'></div>");

Let’s take a look at a quick example.


Let’s say we have the following HTML:

<div id="div1">
  <p class="p2">This is paragraph 2</p>
  <p class="p3">This is paragraph 3</p>
</div>

If we want to insert a new paragraph before paragraph with class “p2”, then we can use the jQuery before() method to do this with the following jQuery code.

$(".p2").before("<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

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

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

If you want to insert HTML after another element, we can do this with the jQuery after() method.

Using the jQuery before() Method to insert HTML before a Div on a Click

In this example, we will have a div that will be a box. We will then let the user insert a new box we create using the before() method as many times as they want.

Here is the HTML code:


<style>.box{ float: left; width: 50px; height: 50px; background: #7bbfa2; margin-right: 10px; margin-bottom: 10px; }</style>
<div id="div2">
  <div id="first-box" class="box" style="border: 2px solid #000;"></div>
</div><div class="clear"></div>
<div id="click-me">Insert new box</div>

The new code to create a box will just be a new div with the class “box”.

We will change the new box background color using the css() method and add it as a style attribute.

Also note that our original box will have a black border on it so we can keep track of it as we add boxes.

Here is the jQuery and JavaScript code:

$("#click-me").click(function(){
  //Create a random color
  var randomColor = "#" + (Math.floor(Math.random()*16777215).toString(16));
  //Create a new div with class box, and add the new color to it as a bg  
  $('#first-box').before("<div class='box' style='background: " + randomColor + ";'></div>");
});

The final code and output for this example of using the jQuery before() method to create a bunch of new box divs is below:

Code Output:

Insert new box

Full Code:


<style>.box{ float: left; width: 50px; height: 50px; background: #7bbfa2; margin-right: 10px; margin-bottom: 10px; }</style>
<div id="div2">
  <div id="first-box" class="box" style="border: 2px solid #000;"></div>
</div><div class="clear"></div>
<div id="click-me">Insert new box</div>

<script>

$("#click-me").click(function(){
  var randomColor = "#" + (Math.floor(Math.random()*16777215).toString(16));
  $('#first-box').before("<div class='box' style='background: " + randomColor + ";'></div>");
});

</script>

Hopefully this article has been useful for you to understand how to use the jQuery before method.

Other Articles You'll Also Like:

  • 1.  jQuery keyup Method
  • 2.  jQuery mouseup – An Interactive Example of the jQuery mouseup Method
  • 3.  Using jQuery to Swap an Image on Hover
  • 4.  Using jQuery to Capitalize the First Letter of a String
  • 5.  Using jQuery to Append Text to textarea Field
  • 6.  Using jQuery to Remove Class from an HTML Element
  • 7.  How to Uncheck All Checkboxes in jQuery
  • 8.  Get Selected Option from Dropdown Using jQuery
  • 9.  Using jQuery to Set Visibility
  • 10.  jQuery focus – Changing Form Styles with the focus() 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