• 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 Change Inner HTML

Using jQuery to Change Inner HTML

December 6, 2021 Leave a Comment

To change the inner HTML of an element using jQuery, the simplest way is to use the jQuery html() method:

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

Let’s say I have the following HTML:

<div id="div">Hello!</div>

If I want to change the inner html inside the div from “Hi!” to “Bye!”, I can use the jQuery html() method to do this with the following Javascript code.

$("#div").html("Bye!");

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

jQuery("#div").html("Bye!");

We can also use the jQuery text() method to change the inner html of a HTML element only if the new content will only be text.

Change Inner HTML of Element 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 change the html of an element.

Let’s say we have the following code and we want to add html content to our paragraph.

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

If we want to change the html content of the paragraph and add some bold text, 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(){
    $("#p-to-change").html("The html is now changed with some <strong>bold content</strong>.");
});

The final code and output for this example of how to change the inner html of an element using the jQuery html() method and Javascript is below:

Code Output:

Click here to update the inner HTML inside the paragraph.

We are going to change the HTML in this paragraph.

Full Code:

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

<script>

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

</script>

Add Elements to Inner HTML of Element with jQuery

If you want to add multiple elements to an element, we can do that as well with the jQuery html() method.

To add multiple elements or a more complex HTML structure to a HTML element, we just need to build the 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 update the inner HTML inside the div.</div>
  <div id="div-to-change">We are going to change the HTML in 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 the first paragraph added.</p><p>This is another paragraph to add.</p><p>And we add a third.</p>");
});

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

Code Output:

Click here to update the inner HTML inside the div.
We are going to change the HTML in this div.

Full Code:


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

<script>

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

</script>

Hopefully this article has been useful for you to understand how to use jQuery to change the inner HTML of a element.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Get the Current URL
  • 2.  jQuery rotate – How to Rotate an Image using jQuery
  • 3.  jQuery focusin – Changing the Background Color with the focusin() Method
  • 4.  jQuery prependTo – Insert Element As First Child
  • 5.  jQuery on change
  • 6.  jQuery before – Insert HTML Before Another Element
  • 7.  How to Filter a List of Divs with a Text Input Bar Using Javascript
  • 8.  Using jQuery to Check if Checkbox is Checked
  • 9.  How to Set All Checkbox to Checked in jQuery
  • 10.  How to Set Checkbox Checked With jQuery

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