.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.
Remember
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")
Select elements, then get combined text or set escaped text.
Select the element(s) whose text you need.
Call .text() with no args to read the combined string.
Call .text(value) to replace contents with escaped plain text.
Or pass a function (1.4+) that returns the new text per element.
Piece
Required?
Role
jQuery collection
Yes
Elements to read or update
text
No*
String, Number, or Boolean to set
function
Alt*
(index, oldText) => newText (1.4+)
Return (get)
—
Combined string
Return (set)
—
jQuery collection for chaining
* Omit arguments to get; pass text or a function to set.
Pseudocode
Pseudocode
// Getter
parts = []
for each element in matched set:
parts.push(all descendant text)
return parts joined as one string
// Setter
for each element in matched set:
clear children
append createTextNode(String(value)) // HTML escaped
return matched set
// Function setter (1.4+)
for each element (index i):
newText = fn(i, oldText)
set text to newText
Reference
Cheat Sheet
Keep these facts handy while reading or writing text.
Topic
Detail
Syntax
.text() · .text(value) · .text(fn)
Since
1.0 (function form 1.4)
Getter
All matches + descendants combined
Setter
Escapes HTML (safe for user strings)
vs .html()
Plain text vs markup
vs .val()
Element text vs form value
XML
Works in HTML and XML documents
Whitespace
May vary slightly by browser parser
Try it
Live Preview
Get or set text on a tiny demo tree and see escaping vs markup.
Test Paragraph.
Placeholder
one
two
three
Browser demo of .text() — same idea as the examples below.
Result
Ready — pick an action.
Code
jQuery Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — Get Combined Text
Read all descendant text from a container as one string.
text. Creates a text node — tags are visible characters.
html. Parses markup — a real <b> element appears.
Security. Untrusted strings → .text().
Trusted markup. Only then consider .html().
Edge Cases & Pitfalls
Check these before reading or writing text in real pages.
forms
Inputs need .val()
.text() is the wrong API for input / textarea values.
getter
All matches are combined
Unlike .html(), the getter joins every matched element’s text.
whitespace
Whitespace can differ
Browsers may vary newlines and spaces in the returned string.
overwrite
Setter replaces all children
Nested elements are removed when you set text on a parent.
types
Numbers and booleans
Non-strings are converted to their string form when setting.
Compatibility
Browser Support
.text() is a core jQuery method (since 1.0; function form 1.4). It needs a browser DOM with jQuery loaded.
✓ jQuery 1.0+ · Browsers only
jQuery .text()
Works in Chrome, Firefox, Safari, Edge, and other browsers with jQuery. Not for Node.js without a DOM.
100%Browser support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.text()Browsers
Bottom line: Use .text() in the browser for safe plain-text get/set. Prefer it over .html() for user-supplied strings.
Analysis
Performance Notes
Approach
Cost
Best for
.text() get
Low–medium
Reading labels / combined text
.text(str) set
Low
Safe updates without parsing HTML
.html(str)
Higher
Trusted markup only
Setting text is usually cheaper and safer than parsing HTML. Getting text from a huge tree walks many nodes — prefer a narrower selector when possible.
Remember
Key Takeaways
Role: get combined text or set escaped plain text.
Safe: setter escapes HTML — prefer for user strings.
vs html / val: plain text vs markup vs form values.
Getter: joins every matched element’s text.
One line:$(sel).text() reads plain text; .text(str) writes it safely without parsing 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.