• 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
  • VBA
  • About
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.  jQuery next – Get the Next Element of a Div
  • 2.  jQuery first() – Get the First Element
  • 3.  Using jQuery to Get the Bottom Position of Element
  • 4.  Using jQuery to Get the Border Width of an Element
  • 5.  Using jQuery to Add Option to Select List
  • 6.  Using jQuery to Convert a String to Uppercase
  • 7.  jQuery mouseup – An Interactive Example of the jQuery mouseup Method
  • 8.  jQuery fadeIn – Fading In Element in HTML
  • 9.  Using jQuery to Get the Margin of an Element
  • 10.  jQuery insertAfter – Insert Element After Another 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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy