Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery html() method

jQuery html() method

jQuery HTML/CSS methods jQuery HTML/CSS methods

Example

Change the content of all

element's content changes:

$

(

"

button

"

)

.

click

(

function

(

)

{

$

(

"

p

"

)

.

html

(

"

Hello world!

"

)

;

}

)

;

Try it »


Definition and Usage

The html() method sets or returns the content (innerHTML) of the selected elements.

When this method is used toReturncontent, it returns the content of the first matched element.

When this method is used toSettingscontent, it overwrites the content of all matched elements.

**Tip:** To set or return only the text content of the selected elements, use text() method.


Syntax

Returning content:

$(

selector

).html()

Setting content:

$(

selector

).html(

content

)

Setting content using a function:

$(

selector

).html(function

(index,currentcontent)

)

Parameters Description
content Required. Specifies the new content of the selected elements (may contain HTML tags).
function(index,currentcontent) Optional. Specifies a function that returns the new content of the selected elements.
  • index - Returns the index position of the element in the set.
  • currentcontent - Returns the current HTML content of the selected element.
  • |

    More examples

    Returning element content
    How to return the content of an element.

    Setting element content using a function
    Use a function to set the content of all selected elements.


    jQuery HTML/CSS methods jQuery HTML/CSS methods