• 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 / Using jQuery to Replace HTML Inside Div

Using jQuery to Replace HTML Inside Div

December 7, 2021 Leave a Comment

To replace the HTML inside a div using jQuery, the simplest way is to use the jQuery html() method:

$("#div").html("Replaced <em>HTML Content</em>");

Let’s say I have the following HTML:

<div id="div">This is some html in the div.</div>

If I want to replace the html inside the div, I can use the jQuery html() method to do this with the following Javascript code.

$("#div").html("This is some new html with <em>some bold</em> text!");

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

jQuery("#div").html("This is some new html with <em>some bold</em> text!");

We can also use the jQuery text() method to replace the text inside a div only if the new content will only be text.

Replace HTML Inside Div On Click with jQuery

The jQuery html() method is very useful when it comes to manipulating web pages.

We can use the jQuery html() method to replace the html inside a div.

Let’s say we have the following code and we want to replace the html content inside our div.

<div id="div">
  <div id="click-me">Click here to update the HTML inside the div.</div>
  <div id="div-to-change">
     <p>We are going to replace the HTML in this div.</p>
  </div>
</div>

If we want to replace the html content of the div and add some bold text to the paragraph, we can do this easily utilizing both the jQuery click() method and jQuery html() method.

Below is the Javascript code which will allow the user to be change the text and html content inside the div.

$("#click-me").click(function(){
    $("#div-to-change").html("<p>The html is now changed with some <strong>bold content</strong>.</p>");
});

The final code and output for this example of how to replace the html inside our div using the jQuery html() method and Javascript is below:

Code Output:

Click here to update the HTML inside the div.

We are going to replace the HTML in this div.

Full Code:

<div class="html-code-output">
<div id="div">
   <div id="click-me">Click here to update the HTML inside the div.</div>
   <div id="div-to-change">
     <p>We are going to replace the HTML in this div.</p>
   </div>
</div>
</div>

<script>

$("#click-me").click(function(){
    $("#div-to-change").html("<p>The html is now changed with some <strong>bold content</strong>.</p>");
});

</script>

Replacing Multiple Elements Inside a Div with jQuery

If you want to replace multiple elements inside a div, we can do that as well with the jQuery html() method.

If you want to change the HTML structure within a div, or add a complex HTML structure to a HTML element, we just need to build a string and then pass it to the jQuery html() method.

Let’s say we have the following code and we want to add multiple paragraphs to a div.

<div id="div">
  <div id="click-me">Click here to replace the HTML inside the div.</div>
  <div id="div-to-change">We are going to replace the HTML inside this div.</div>
</div>

If we want to change the html content of the div and add multiple paragraphs, we can do this easily by building a string with all of the HTML elements we want, and then passing it to the jQuery html() method.

Below is the Javascript code which will allow us to add multiple paragraphs and change the inner html of our div.

$("#click-me").click(function(){
    $("#div-to-change").html("<p>This is one paragraph.</p><p>This is another paragraph.</p><p>And now a third paragraph.</p>");
});

The final code and output for this example of how to replace the html of a div and add multiple elements with a click using the jQuery html() method and Javascript is below:

Code Output:

Click here to replace the HTML inside the div.
We are going to replace the HTML inside this div.

Full Code:

<div class="html-code-output">
<div id="div">
  <div id="click-me">Click here to replace the HTML inside the div.</div>
  <div id="div-to-change">We are going to replace the HTML inside this div.</div>
</div>
</div>

<script>

$("#click-me").click(function(){
     $("#div-to-change").html("<p>This is one paragraph.</p><p>This is another paragraph.</p><p>And now a third paragraph.</p>");
});

</script>

Hopefully this article has been useful for you to understand how to use jQuery to replace the HTML inside a div.

Other Articles You'll Also Like:

  • 1.  How to Check if Element Exists Using jQuery
  • 2.  jQuery hasClass – How to Check if an Element Has a Certain Class
  • 3.  Using jQuery to Remove Attribute from HTML Element
  • 4.  jQuery insertAfter – Insert Element After Another Element
  • 5.  jQuery Display None
  • 6.  Using jQuery to Get the Top Position of Element
  • 7.  How to Use jQuery to Check if an Element is Visible
  • 8.  Using jQuery to Remove the Id of a Div
  • 9.  jQuery mouseout – An Interactive Example of the mouseout Method
  • 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