Easy Tutorial
❮ Misc Deferred Progress Traversing Last ❯

jQuery text() Method

jQuery HTML/CSS Methods

Example

Set the text content for all <p> elements:

$("button").click(function(){
    $("p").text("Hello world!");
});

Definition and Usage

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

When used to return content, it returns the text content of all matched elements (HTML tags are removed).

When used to set content, it overwrites the content of all matched elements.

Tip: To set or return the innerHTML of the selected elements (text + HTML tags), use the html() method.


Syntax

Return text content:

Set text content:

Set text content using a function:

Parameter Description
content Required. Specifies the new text content for the selected elements. <br> <br> Note: Special characters are encoded.
function(index, currentcontent) Optional. Specifies a function that returns the new text content for the selected elements. index - Returns the index position of the element in the set. <br> <br> currentcontent - Returns the current content of the selected element.

More Examples

Return Text Content

Set Text Content Using a Function


jQuery HTML/CSS Methods

❮ Misc Deferred Progress Traversing Last ❯