If you're using a master page or user control where the clientID will be different than what you defined in your .aspx page or possible that another element shares the same class, another option is:
$("span[id$='lblError']").text('hello');
To specifically answer the question though as noted already .text()
is the most appropriate option as all 3 will work. It just semantically makes sense to use .text()
to insert text rather than .html()
since you are not inserting HTML.
In terms of what actually happens would be another part of the answer now that I think about it a little more, and you can see this yourself by examining the jQuery script itself:
.text() calls javascript function createTextNode
with the string passed in and appends to DOM and does little else. //12 lines of code
.html() does more processing for cleaning/processing of the string that is passed in. Also looks to have some code in place to prevent memory leaks. Also uses the innerHTML
function of the browser. //38 lines of code
One last note on the difference between .text()
and .html()
as seen on the API docs to differentiate the two is this line:
Unlike the .html() method, .text() can be used in both XML and HTML documents.