• 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 Text of HTML Element

Using jQuery to Change Text of HTML Element

December 3, 2021 Leave a Comment

To change text using jQuery, the simplest way is to use the jQuery text() method:

$("#element").text("Changed Text");

If your element will contain html content, then you should use the jQuery html() method.

$("#element").html("Changed <em>Text</em>");

Let’s say I have the following HTML:

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

If I want to change the text inside the paragraph from “Hello!” to “Good bye!”, I can use the jQuery text() method to do this with the following Javascript code.

$("#p").text("Good bye!");

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

jQuery("#p").text("Good bye!");

We can also use the jQuery html() method to change the content of an HTML element.

$("#p").html("Good bye!");

While the text() method is very simple, the html() method gives us the ability to insert html into the element, which gives us more options when styling the content.

Changing the Text on Click Using jQuery

Many times when creating a web page and the user experience, we want to change the content of an element after an interaction with another element on the web page.

To change the text of an element on click using jQuery, we can combine the text() method with a click event.

Let’s say I have the following HTML, and I want to give the user the ability to change the text of the paragraph below from “Welcome!” to “Home”.

<div id="div1">
  <div id="click-me">Click here to update paragraph text.</div>
  <p class="p">Welcome!</p>
</div>

We can utilize both the jQuery click() method and jQuery text() method to change the text from “Welcome!” to “Home”.

Below is the Javascript code which will allow the user to be able to update the text in the paragraph:

$("#click-me").click(function(){
    $(".p").text("Home");
}); 

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

Code Output:

Click here to update paragraph text.

Welcome!

Full Code:

<div class="html-code-output">
<div id="div1">
  <div id="click-me">Click here to update paragraph text.</div>
  <p class="p">Welcome!</p>
</div>
</div>

<script>

$("#click-me").click(function(){
    $(".p").text("Home");
});

</script>

Using the html() Method to Change Text on Click

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 and text of an element.

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

<div id="div">
  <div id="click-me">Click here to update the text inside the span.</div>
  <p>We are going to add html to this <span id="span-with-text">span with content</span>.</p>
</div>

If we want to make the text “content” bold, 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 of the span.

$("#click-me").click(function(){
    $("span-with-text").html("span with <strong>content</strong>");
});

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

Code Output:

Click here to update the text inside the span.

We are going to add html to this span with content.

Full Code:

<div class="html-code-output">
<div id="div">
  <div id="click-me">Click here to update the text inside the span.</div>
  <p>We are going to add html to this <span id="span-with-text">span with content</span>.</p>
</div>
</div>

<script>

$("#click-me").click(function(){
    $("span-with-text").html("span with <strong>content</strong>");
});

</script>

Hopefully this article has been useful for you to understand how to use jQuery to change the text of an element.

Other Articles You'll Also Like:

  • 1.  jQuery val Method – Get the Value from an Input Field
  • 2.  jQuery get img src – Get the Source of an Image Using jQuery
  • 3.  Using jQuery to Set a Radio Button to Checked
  • 4.  How to Set All Checkbox to Checked in jQuery
  • 5.  Using jQuery to Remove Attribute from HTML Element
  • 6.  Using jQuery to Increment Value on Click
  • 7.  Using jQuery to get Textarea Value
  • 8.  Using jQuery to Get Element by ID
  • 9.  Using jQuery to Show a Div
  • 10.  Using jQuery to Scroll to Top

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