jQuery in Action
Setting element content
69
We can also set or get only the text contents of elements. The
text()
command,
when used without parameters, returns a string that's the concatenation of all
text. For example, let's say we have the following
text. For example, let's say we have the following
HTML
fragment:
<ul id="theList">
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
The statement
var text = $('#theList').text();
results in variable
text
being set to
OneTwoThreeFour
.
We can also use the
text
command to set the text content of the wrapped ele-
ments. The syntax for this format is as follows:
Command syntax: html
html(text)
Sets the passed HTML fragment as the content of all matched elements
Parameters
text
(String) The HTML fragment to be set as the element content
Returns
The wrapped set
The wrapped set
Command syntax: text
text()
Concatenates all text content of the wrapped elements and returns it as the result of
the command
Parameters
none
Returns
The concatenated string
The concatenated string