.text()

Get the combined text contents of each element in the set of matched elements, including their descendants. Set the content of each element in the set of matched elements to the specified text.

.text()🡢 String

Unlike the .html() method, .text() can be used in both XML and HTML documents. The result of the .text() method is a string containing the combined text of all matched elements. (Due to variations in the HTML parsers in different browsers, the text returned may vary in newlines and other white space.) Consider the following HTML:

<div class="demo-container">
  <div class="demo-box">Demonstration Box</div>
  <ul>
    <li>list item 1</li>
    <li>list <strong>item</strong> 2</li>
  </ul>
</div>

The code $( "div.demo-container" ).text() would produce the following result:

Demonstration Box list item 1 list item 2

The .text() method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, use the .val() method. To get the value of a script element, use the .html() method.

As of jQuery 1.4, the .text() method returns the value of text and CDATA nodes as well as element nodes.

Find the text in the first paragraph (stripping out the html), then set the html of the last paragraph to show it is just text (the red bold is gone).

JS
<p><b>Test</b> Paragraph.</p>
<p></p>
CSS
p {
  color: blue;
  margin: 8px;
}
b {
  color: red;
}
HTML
var str = $("p").first().text();
$("p").last().html(str);
DEMO

.text(text)🡢 jQuery

text String, Number, BooleanThe text to set as the content of each matched element. When Number or Boolean is supplied, it will be converted to a String representation.

.text(function(indexInteger, textString))🡢 String

function(indexInteger, textString) FunctionA function returning the text content to set. Receives the index position of the element in the set and the old text value as arguments.

Unlike the .html() method, .text() can be used in both XML and HTML documents.

We need to be aware that this method escapes the string provided as necessary so that it will render correctly in HTML. To do so, it calls the DOM method .createTextNode(), does not interpret the string as HTML. Consider the following HTML:

<div class="demo-container">
  <div class="demo-box">Demonstration Box</div>
  <ul>
    <li>list item 1</li>
    <li>list <strong>item</strong> 2</li>
  </ul>
</div>

The code $( "div.demo-container" ).text( "<p>This is a test.</p>" ); will produce the following DOM output:

<div class="demo-container">&lt;p&gt;This is a test.&lt;/p&gt;</div>

It will appear on a rendered page as though the tags were exposed, like this:

<p>This is a test</p>

The .text() method cannot be used on input elements. For input field text, use the .val() method.

As of jQuery 1.4, the .text() method allows us to set the text content by passing in a function.

$("ul li").text(function (index) {
  return "item number " + (index + 1);
});

Given an unordered list with three <li> elements, this example will produce the following DOM output:

<ul>
  <li>item number 1</li>
  <li>item number 2</li>
  <li>item number 3</li>
</ul>

Add text to the paragraph (notice the bold tag is escaped).

JS
<p>Test Paragraph.</p>
CSS
p {
  color: blue;
  margin: 8px;
}
HTML
$("p").text("<b>Some</b> new text.");
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)