text() in jQuery
Using this method we can retrieve all the text contents of each element including their descendants, or set the text contents of the matched elements.
This method cannot be used on form inputs or scripts.
Syntax 1 - Get the combined text contents of each element in the set of matched elements, including their descendants.
Syntax 2 - Set the content of each element in the set of matched elements to the specified text.
- text - The text to set as the content of each matched element.
Syntax 3 - A function returning the value to set
- index - Index position of the element in the set.
- currentTextString - The old text value.
text() method convert like below
< to <
> to >
Example 3 - get old text and set new text
Example 4 - Execution steps
Example 5 - Get drop down text and selected text
Difference between html() and text()
Using text() method it retrieves only the text from an element, but using html() method both the text as well as the HTML markup can be retrieved from the element.