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.

New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources