jQuery text() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

.text() gets or sets plain text content (since jQuery 1.0). The getter returns the combined text of all matched elements, including descendants. The setter escapes HTML via createTextNode — tags are shown as characters, not markup. Works in both HTML and XML.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
var s = $("div.demo").text()     // get combined text
$("p").text("Hello")             // set plain text
$("p").text("<b>Hi</b>")         // tags escaped — not bold

// vs html (parses markup) and val (form values):
$("p").html("<b>Hi</b>")
$("input").val("Hello")
CallWhat it does
text()Learn jQuery .text(): get combined text contents, set escaped plain text safely, callback form, vs html and val, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select the element(s) whose text you need.
Step 2Call .text() with no args to read the combined string.
Step 3Call .text(value) to replace contents with escaped plain text.

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Get Combined Text

Read all descendant text from a container as one string.

jQuery
// <div class="demo">
//   Demonstration Box
//   <ul><li>list item 1</li><li>list item 2</li></ul>
// </div>
console.log($("div.demo").text());
Try It Yourself

How It Works

  1. Select. The container and all descendants are in scope.
  2. Collect. Every text node is gathered.
  3. Combine. One string is returned (whitespace may vary by browser).

Example 2 — Official Demo: Set Escaped Text

Pass a string with tags — they appear as characters, not HTML.

jQuery
$("p").text("<b>Some new text.</b>");
// Visible as: <b>Some new text.</b>  (not bold)
Try It Yourself

How It Works

  1. Replace children. Old content is cleared.
  2. Text node. jQuery uses createTextNode.
  3. Escaped. <b> is literal text, not a bold element.

Example 3 — Official Demo: Strip Then Show

Copy text from the first paragraph into the last via .html().

jQuery
var str = $("p").first().text();
$("p").last().html(str);
Try It Yourself

How It Works

  1. Read. First p text strips inner <b>.
  2. Plain string. No markup remains in str.
  3. Write. Last p gets that string via .html().

Frequently Asked Questions

With no arguments, .text() returns the combined text of all matched elements, including descendants. With an argument, it replaces children with a text node — HTML is escaped, not parsed. Works in HTML and XML. Available since jQuery 1.0; function form since 1.4.
.text() reads/writes plain text only. .html() reads/writes markup. Setter .text() escapes tags; .html() parses them. Prefer .text() for user strings.
.text() is for element contents (div, p, span). .val() is for form controls (input, textarea, select). Do not use .text() on inputs.
Yes. Unlike .html() (first element only), .text() concatenates text from every matched element and their descendants into one string.
Yes, since 1.4. .text(function(index, text)) runs once per element; return the new string. this is the current DOM element.
Yes. It uses createTextNode, so angle brackets show as characters instead of markup. Prefer .text() over .html() for names, comments, and messages.

Next: .unwrap() Method

Insert content after each matched element.

.unwrap() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful