jQuery Document Operation - html() Method
Example
Set the content of all p elements:
$(".btn1").click(function(){ $("p").html("Hello <b>world</b>!"); });
Definition and Usage
The html() method returns or sets the content (inner HTML) of the selected element.
If this method does not set any parameters, it will return the current content of the selected element.
Return element content
When using this method to return a value, it will return the content of the first matching element.
Syntax
$(selector).html()
Set element content
When using this method to set a value, it will override the content of all matching elements.
Syntax
$(selector).html(content)
Parameters | Description |
---|---|
content | - Optional. Specifies the new content of the selected elements. This parameter can include HTML tags. |
Use a function to set element content
Use a function to set the content of all matching elements.
Syntax
$(selector).html(function(index, oldcontent))
Parameters | Description |
---|---|
function(index, oldcontent) |
Specifies a function that returns the new content of the selected elements.
|