jQuery html()

The jquery html() method is used to set or get the HTML content of an element in the HTML document.

In this tutorial, you will learn about jQuery html() method, its syntax and usage, with examples.

Syntax – jQuery html()

Following is the syntax to use jQuery html() method

ADVERTISEMENT

Get HTML content

To get the HTML content of an Element in the HTML document,

var x = $("htmlElement").html();

Set HTML content

To set or modify HTML content of an Element in the HTML document,

$("htmlElement").html("modified html content.");

Examples – jQuery html()

The following examples help you understand the usage of jQuery html() method.

1. Example – Get HTML value of an HTML Element

In the following jQuery html() example, there is a button and paragraph. When the button is clicked, we shall get the HTML text of paragraph, referenced by id in the javascript, and display it through an alert.

2. Example – Set HTML value of an HTML Element

In the following jQuery html() example, there is a button and paragraph. When the button is clicked, we shall use html() method to set different HTML content for the paragraph element. In the example, elements are selected using their ids.

Conclusion

In this jQuery Tutorial, we have learnt jQuery html() method : Syntax and Usage with examples.