What is the difference between html() and text() in JQuery?

The JQuery html() and text() methods are two methods that you can use to get or set the contents of an HTML element. The difference between them is stated below:

html() is used to return or change the html and text content of an element.

text() can only return or change the text content of an element.

Syntax

html() syntax

//SET THE HTML CONTENT
$([selector]).html([content])

//RETURN THE HTML CONTENT
$([selector]).html()

text() syntax

//SET THE TEXT CONTENT
$([selector]).text([content])

//RETURN THE TEXT CONTENT
$([selector]).text()

Parameters

html(): The content is newly specified, and can contain HTML tags.
text(): The content is newly specified, and cannot contain HTML tags.

Code

In the program above, the html() method was used on the first paragraph tag to set it to a new heading tag with new text content. The text() method was used on the second paragraph to change its text content.

Free Resources