• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
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.  jQuery fadeIn – Fading In Element in HTML
  • 2.  Using jQuery to Hide Element by Class
  • 3.  Using jQuery to Capitalize the First Letter of a String
  • 4.  Using jQuery to Add Option to Select List
  • 5.  How to use jQuery to Make an Input Field readonly
  • 6.  jQuery slideUp – How to Slide Up and Hide an Element Using jQuery
  • 7.  jQuery focus – Changing Form Styles with the focus() Method
  • 8.  Resizing an Image Using jQuery
  • 9.  Examples of Text Animations Using jQuery
  • 10.  Using jQuery to Remove Class from an HTML Element

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 *

Copyright © 2023 · The Programming Expert · About · Privacy Policy