.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")
Call
What 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
Parameter
Description
Step 1
Select the element(s) whose text you need.
Step 2
Call .text() with no args to read the combined string.
Step 3
Call .text(value) to replace contents with escaped plain text.
Return Value
Return Value
Description
Value / undefined
Getters 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.
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.