What the difference between text() and html() functions in jQuery ?
$(\"#div\").html(\'Linkhello\
.text()
will give you the actual text in between HTML tags. For example, the paragraph text in between p
tags. What is interesting to note is that it will give you all the text in the element you are targeting with with your $
selector plus all the text in the children elements of that selected element. So If you have multiple p
tags with text inside the body element and you do a $(body).text()
, you will get all the text from all the paragraphs. (Text only, not the p
tags themselves.)
.html()
will give you the text and the tags. So $(body).html()
will basically give you your entire page HTML page
.val()
works for elements that have a value
attribute, such as input
.
An input
does not have contained text or HTML and thus .text()
and .html()
will both be null
for input
elements.