html() in jQuery

html() in jQuery

Using this method it can get the HTML contents of the first element in the set of every matched element.

Browser's innerHTML property is used by this method.

Syntax 1 - Get the HTML contents of the first element in the set of matched elements.

$(selector).html()

Syntax 2 - Set the HTML contents of each element in the set of matched elements.

$(selector).html(htmlString)

  • htmlString - A string of HTML to set as the content of each matched element.
Syntax 3 - A function returning the value to set

$(selector).html(function(index, currentHtmlString))

  • index - Index position of the element in the set.
  • currentHtmlString - The old HTML value.

Example 1 - get html string

Try Yourself

Example 2 - set html string

Try Yourself

Example 3 - get old html string and set new html string

Try Yourself

Example 4 - Execution steps

Try Yourself

Difference between html() and text()

Using text() method it retrieves only the text from an element, but using html() method both the text as well as the HTML markup can be retrieved from the element.

Example

Try Yourself