Content API
txt()
Sets or gets the text content of the matched DOM elements and their descendants.
-
Accepts an argument as a string and an optional second argument, which can be one of the following:
ArgumentDescription'before'Places the text content before the matched DOM element (before the opening tag).'after'Places the text content after the matched DOM element (after the closing tag).'begin'Places the text content immediately after the matched DOM element's opening tag (before the first child).'end'Places the text content right before the matched DOM element's closing tag (after the last child).
-
If the second argument is specified, DOM elements with the "data-remove" attribute matching existing DOM elements will be removed.
This is useful for scenarios such as adding new content and removing outdated elements, like replacing old pagination with updated content. - Cannot read styles or retrieve CSS, returns the contents of hidden elements and the contents of "<script>" and "<style>" tags.
- If no arguments are passed, it returns a string if a single matched DOM element, and an array of strings if multiple matched DOM elements.
Example:
$('div').txt('');
$('div').txt(null);
// Removes the text content from the <div> element.
$('div ?').txt('');
$('div ?').txt(null);
// Removes the text content from all <div> elements.
$('div ?').txt('cat');
// <div>cat</div>
// <div>cat</div>
// <div>cat</div>
$('div').txt('book');
// <div>book</div>
// <div>cat</div>
// <div>cat</div>
$('div').txt();
// 'book'
$('div ?').txt();
// ['book', 'cat', 'cat']
$('div').txt('-cat-', 'before');
// -cat-
// <div>book</div>
// <div>cat</div>
// <div>cat</div>
$('div').txt('-cat-', 'after');
// <div>book</div>
// -cat-
// <div>cat</div>
// <div>cat</div>
$('div').txt('-cat-', 'begin');
// <div>-cat-book</div>
// <div>cat</div>
// <div>cat</div>
$('div').txt('-cat-', 'end');
// <div>book-cat-</div>
// <div>cat</div>
// <div>cat</div>