Javatpoint Logo
Javatpoint Logo

jQuery text()

The jQuery text() method is used to set or return the text content of the selected elements.

To return content: When this method is used to return content, it returns the combined text content of all matched elements without the HTML markup.

To set content: When this method is used to set content, it overwrites the content of all matched elements.

Difference between jQuery text() method and jQuery html() method

Sometimes, this confusion is occurred because both of the methods are used to set or return the html content. But, the jQuery text() method is different from html() method.

Following is the main differences:

  • The jQuery text() method is used to set or return html content without HTML markup while, html() method is used to set or return the innerHtml (text + HTML markup).
  • The jQuery text() method can be used in both XML and HTML document while jQuery html() method can't.

Syntax:

To return text content:

To set text content:

To set text content using a function:

Parameters of jQuery text() method

Parameter Description
Content It is a mandatory parameter. It specifies the new text content for the selected elements. The special characters will be encoded in this parameter.
Function (index,currentcontent) It is an optional parameter. It specifies the function that returns the new text content for the selected elements.
  • Index: It provides the index position of the element in the set.
  • Currentcontent: It provides the current content of the selected elements.

Example of jQuery text() method

Let's take an example to demonstrate the effect of jQuery text() method.

A simple example to return content:

Test it Now

Output:

Hello! javatpoint.com

Hello! javatpoint.com

Another example of jQuery text() method

An example for set content:

Test it Now

Output:

Hello Guys!

Looking for online training....

Next TopicjQuery val()





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA