What the difference between text() and html() functions in jQuery ?
$(\"#div\").html(\'Linkhello\
Use .text(…) when you intend to display the value as a simple text.
Use .html(…) when you intend to display the value as a html formatted text (or HTML content).
You should definitely use .text(...) when you’re not sure that your text (e.g. coming from an input control) do not contain characters/tags that has special meaning in HTML. This is really important because this could result in the text will not display properly but it could also cause that an unwanted JS script snippet (e.g. coming from another user input) to be activated.
The text()
method entity-escapes any HTML that is passed into it. Use text()
when you want to insert HTML code that will be visible to people who view the page.
Technically, your second example produces:
<a href="example.html">Link</a><b>hello</b>
which would be rendered in the browser as:
<a href="example.html">Link</a><b>hello</b>
Your first example will be rendered as an actual link and some bold text.
Strange that no-one mentioned the Cross Site scripting prevention benefit of .text()
over .html()
(Although others have just mentioned that .text()
escapes the data).
It's always recommended to use .text()
when you want to update data in DOM which is just data / text for the user to see.
.html()
should be mostly used to get the HTML content within a div.
((please update if necessary, this answer is a Wiki))
.text()
or .html()
?Answer: .html()
is faster! See here a "behaviour test-kit" for all the question.
So, in conclusion, if you have "only a text", use html()
method.
Note: Doesn't make sense? Remember that the .html()
function is only a wrapper to .innerHTML
, but in the .text()
function jQuery adds an "entity filter", and this filter naturally consumes time.
Ok, if you really want performance... Use pure Javascript to access direct text-replace by the nodeValue
property.
Benchmark conclusions:
.html()
is ~2x faster than .text()
..innerHTML
is ~3x faster than .html()
..nodeValue
is ~50x faster than .html()
, ~100x than .text()
, and ~20x than .innerHTML
.PS: .textContent
property was introduced with DOM-Level-3, .nodeValue
is DOM-Level-2 and is faster (!).
See this complete benchmark:
// Using jQuery:
simplecron.restart(); for (var i=1; i<3000; i++)
$("#work").html('BENCHMARK WORK');
var ht = simplecron.duration();
simplecron.restart(); for (var i=1; i<3000; i++)
$("#work").text('BENCHMARK WORK');
alert("JQuery (3000x): \nhtml="+ht+"\ntext="+simplecron.duration());
// Using pure JavaScript only:
simplecron.restart(); for (var i=1; i<3000; i++)
document.getElementById('work').innerHTML = 'BENCHMARK WORK';
ht = simplecron.duration();
simplecron.restart(); for (var i=1; i<3000; i++)
document.getElementById('work').nodeValue = 'BENCHMARK WORK';
alert("Pure JS (3000x):\ninnerHTML="+ht+"\nnodeValue="+simplecron.duration());
var v = "全名";
$("#div").html(v); //display as "全名"
$("#div").text(v); //display as "全名"
text() – This method sets or returns the text content of elements selected. html() – This method sets or returns the content of elements selected. Refer example here : https://www.tutorialspoint.com/online_jquery_editor.php
The different is .html()
evaluate as a html, .text()
avaluate as a text.
Consider a block of html
HTML
<div id="mydiv">
<div class="mydiv">
This is a div container
<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
</ul>
a text after ul
</div>
</div>
JS
var out1 = $('#mydiv').html();
var out2 = $('#mydiv').text();
console.log(out1) // This output all the html tag
console.log(out2) // This is output just the text 'This is a div container Link 1 Link 2 a text after ul'
The illustration is from this link http://api.jquery.com/text/