Home » jQuery » Difference between .html() and .text() functions in jQuery

Difference between .html() and .text() functions in jQuery

Difference between .html() and .text() functions in jquery:

.html() can be used to get the contents of inside an element. If your selector expression matches more than one element, only the first match will have its HTML content returned:

here is an example:

  • list sample 1
  • list sample 2
  • list sample 3

now if you are using .html():
$(‘.html-function-demo’).html();

your output is:

  • list sample 1
  • list sample 2
  • list sample 3

now if you are using .text():
$(‘.html-function-demo’).text();

your output is:
list sample 1 list sample 2 list sample 3

due to different properties of HTML Parsers of your browser, the output for your function varies in the manner that the output will be in one line or continuous one or with spaces and linebreaks.

Both functions return strings as their output.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.