jQuery text() Method

jQuery text() method sets or returns the text content of selected elements.

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

Syntax

Here is a text() method syntax that return text content. Here text() method is a string containing the combined text of all selected elements.

$(selector).text();

And this syntax for set text content.

$(selector).text( content );
Parameter Type Description
content String
Number
Boolean
Required. Specifies the new text content for the selected elements.
When Number or Boolean is passed then it will be converted to a String representation.

Example: Return text content

Here, in this example text content return on selected elements.

$(document).ready(function(){
  $("button").click(function(){
    var tx;
    tx = $(".third").text();
    alert(tx);
  });
});

Run it...   »

Example: Set text content

Here, in this example set text content on selected elements.

$(document).ready(function(){
  $("button").click(function(){
    $(".third").text("Last paragraph");
  });
});

Run it...   »