jQuery text() Method

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

In jQuery, the term text generally refers to the textual content of an HTML element. It includes the human-readable content that appears within the element on a web page. This content is what a user sees as plain text and does not include any HTML tags or elements.

The jQuery .text() method is a built-in function provided by the jQuery library, which is a popular JavaScript library used for simplifying HTML manipulation and event handling. The .text() method is used to retrieve or set the text content of HTML elements.

Syntax

The syntax for the jQuery .text() method is as follows:

To get the text content of an element

To set the text content of an element

The .text() method sets the text content of the selected element(s) to the provided content or retrieves the combined text content of the selected element(s) if no content is provided.

Parameters

The jQuery .text() method doesn't take any parameters when used to get the text content of elements. However, when used to set the text content, it takes one parameter, which is the new text content we want to assign to the selected element(s).

Setting Text Content

When we want to set the text content of an element or a group of elements, we use the .text() method.

  • selector: This is a string representing a CSS selector that specifies the element(s) you want to target.
  • newTextContent: This is a string that contains the new text content you want to set for the selected element(s).

Getting Text Content

When we use the .text() method to retrieve the text content of an element, we don't need to provide any parameters. The method will return the text content of the first selected element.

  • selector: This is a string representing a CSS selector that specifies the element(s) you want to target.
  • textContent: This is a variable where the retrieved text content will be stored. It will contain the combined text content of all the selected elements.

Examples

Here are some examples of using the jQuery .text() method along with the expected output for each example.

Example 1: Getting Text Content

jQuery Code

Output

Explanation:

In this example, jQuery's .text() method is used to extract the combined text content from <p> elements. The code selects all <p> elements, retrieves their text, and displays it in the console. The method aggregates text while disregarding HTML tags or formatting, resulting in a single line of combined text output.

Example 2: Setting Text Content

jQuery Code

Output

Explanation:

This example demonstrates how jQuery's .text() method can be employed to update the text content of selected <p> elements. By selecting the element using the "p" selector and using the .text() method with the specified text, the content within the <p> element is replaced.

In the above examples, jQuery selects elements using the $("selector") syntax, where selector is replaced with the appropriate CSS selector. The .text() method is then used to either retrieve or set the text content of the selected element(s).

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

The jQuery .text() and .html() methods are tools for modifying the content of HTML elements, but they differ in how they handle that content. The .text() method is primarily used to get or set the plain text content within HTML elements. When getting content with .text(), it provides the text without any HTML tags, treating them as ordinary text. When setting content using .text(), it replaces the existing text with the provided plain text.

In contrast, the .html() method focuses on the entire HTML content within elements. When retrieving content with .html(), it returns the full HTML markup, including all tags and elements nested within the selected element(s). When setting content with .html(), it replaces the entire content with the provided HTML markup, fully interpreting and rendering any tags and elements within it.

In summary, utilize .text() for plain text manipulation and retrieval, while opting for .html() when working with full HTML content, including tags and elements. However, exercise care when employing .html() with content from untrusted sources to ensure the security of your application.

Here's the tabular difference for the same:

Feature.text() Method.html() Method
PurposeManipulates plain text content within elements.Manipulates HTML content within elements.
Content HandlingTreats content as plain text, ignoring HTML tags.Includes HTML tags and formatting in the content.
UsageRetrieves or replaces plain text content.Retrieves or replaces HTML content.
Example$(selector).text("New text")$(selector).html("<p>New HTML</p>")
Security ConsiderationHelps prevent XSS attacks by treating content as plain text.May expose vulnerabilities if unfiltered user input is inserted as HTML.

Conclusion

  • In conclusion, the .text() method in jQuery helps you get or change the text inside elements. When getting text, it gives you just the words without any fancy formatting. And when you want to change the text, it easily swaps out the old text for new plain text.

  • The syntax of the .text() method involves using the jQuery function to select elements based on CSS selectors. To retrieve text, the method is used without arguments, and to set text, it is used with an argument containing the new text content.

  • The .text() method doesn't require parameters for getting text content, but when setting text, it takes one parameter representing the new text content. It's crucial to distinguish between setting plain text and working with HTML content when choosing between .text() and .html() methods.

  • In contrast, the .html() method retrieves or sets the entire HTML content of elements, including HTML tags and elements. It's suitable when you need to work with HTML structure and content.

  • The jQuery .text() and .html() methods are powerful tools for managing the content of HTML elements in different ways. The choice between them depends on your specific needs and the type of content you're working with.