• 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 after – Insert HTML After Another Element

jQuery after – Insert HTML After Another Element

April 20, 2022 Leave a Comment

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

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

Let’s take a look at a quick example.


Let’s say we have the following HTML:

<div id="div1">
  <p class="p1">This is paragraph 1</p>
  <p class="p2">This is paragraph 2</p>
</div>

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

$(".p2").after("<p class='p3'>This is paragraph 3</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").after("<p class='p3'>This is paragraph 3</p>");

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

Using the jQuery after() Method to insert HTML after 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 after() 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').after("<div class='box' style='background: " + randomColor + ";'></div>");
});

The final code and output for this example of using the jQuery after() 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').after("<div class='box' style='background: " + randomColor + ";'></div>");
});

</script>

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

Other Articles You'll Also Like:

  • 1.  Using jQuery to Remove the Id of a Div
  • 2.  jQuery get first child
  • 3.  Check if Input is Empty Using jQuery
  • 4.  Using jQuery to Get the Parent Div of an Element
  • 5.  Using jQuery to Show a Div
  • 6.  Sort List of Divs with Data Attribute with Radio Buttons Using Javascript
  • 7.  Using jQuery to Delete an Element
  • 8.  jQuery mouseout – An Interactive Example of the mouseout Method
  • 9.  Using jQuery to Get the Bottom Position of Element
  • 10.  Using jQuery to Add Required Attribute to Input Field

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