.text() | jQuery HTML(),text
Description:Setthecontentofeachelementinthesetofmatchedelementstothespecifiedtext.Unlikethe.html()method,.text()canbeusedinbothXMLandHTMLdocuments.WeneedtobeawarethatthismethodescapesthestringprovidedasnecessarysothatitwillrendercorrectlyinHTML.Todoso,itcallstheDOMmethod.createTextNode(),doesnotinterpretthestringasHTML.ConsiderthefollowingHTML:1234567
Description: Set the content of each element in the set of matched elements to the specified text.
Unlike the .html() method, .text() can be used in both XML and HTML documents.
We need to be aware that this method escapes the string provided as necessary so that it will render correctly in HTML. To do so, it calls the DOM method .createTextNode(), does not interpret the string as HTML. Consider the following HTML:
1
2
3
4
5
6
7
<div class="demo-container">
<div class="demo-box">Demonstration Box</div>
<li>list <strong>item</strong> 2</li>
The code $( "div.demo-container" ).text( "<p>This is a test.</p>" ); will produce the following DOM output:
1
...