• 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 text – Set the text of an HTML Element

jQuery text – Set the text of an HTML Element

March 27, 2022 Leave a Comment

We can use the jQuery text() method to set the text of an HTML element.

$("#div1").text("Some text");

Let’s say I have the following HTML:

<div id="div1">This is some text.</div>

If we want to change the text inside the div from “This is some text.” to “This is some new text.”, we can use the jQuery text() method to do this with the following jQuery code.

$("#div1").text("This is some new text.");

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

jQuery("#div1").text("This is some new text.");

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

$("#div1").html("Some <em>Text</em>");

Using the jQuery text method to set the text of a div with a click

To set the text of a div 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 set the text of the div below to “We just added this text using the jQuery text method!”.

<div id="div1">
  <div id="div-to-update"></div>
  <div id="click-me">Add text</div>
</div>

We can utilize both the jQuery click() method and jQuery text() method to set the text to “We just added this text using the jQuery text method!”.

Below is the jQuery code which will allow the user to be able to update the text in the div:

$("#click-me").click(function(){
  $("#div-to-update").text("We just added this text using the jQuery text method!");
});

The final code and output for this example of how to set the text of a div using the jQuery text() method is below:

Code Output:

Add text

Full Code:

<div id="div1">
  <div id="div-to-update"></div>
  <div id="click-me">Add text</div>
</div>

<script>

$("#click-me").click(function(){
  $("#div-to-update").text("We just added this text using the jQuery text method!");
});

</script>

Hopefully this article has been useful for you to understand the jQuery text method.

Other Articles You'll Also Like:

  • 1.  How to use jQuery to Make an Input Field readonly
  • 2.  Using jQuery to Add Class to HTML Element
  • 3.  jQuery width() – Using jQuery to Get the Width of a Div
  • 4.  Using jQuery to Remove Attribute from HTML Element
  • 5.  Using jQuery to Get the Current URL
  • 6.  Using jQuery to Hide Element by Class
  • 7.  jQuery Random Number Generator Between Two Numbers
  • 8.  Get Padding of an Element Using jQuery
  • 9.  jQuery get img src – Get the Source of an Image Using jQuery
  • 10.  Using jQuery to Change Label Text

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