Skip to the content.

$.fn.text([content])

Get or set the text content of every element in the collection.

When called with no arguments, returns the concatenated textContent of every element. When called with a string, number, boolean, or callback, sets the text on every element.

Because the value is written via textContent, any HTML in the input is rendered as literal text. This makes text the safe choice for displaying user-supplied content.

Signatures

text(): string;
text(content: string | number | boolean | ((this: Element, index: number, currentText: string) => string | number | boolean)): this;

Parameters

Returns

When getting, a string formed by concatenating the textContent of every element. When setting, the original Dabby collection for chaining.

Examples

import $ from "dabbyjs";
import "dabbyjs/manipulation/text/text";

// Read the heading
const heading = $("h1").text();
import $ from "dabbyjs";
import "dabbyjs/manipulation/text/text";

// Display a count — number is coerced to string
$(".cart-count").text(3);
import $ from "dabbyjs";
import "dabbyjs/manipulation/text/text";

// Format prices in place
$(".price").text(function (index, current) {
    return "£" + Number(current).toFixed(2);
});

See also