← Back to Index

.html()

Get the HTML contents of the first element in the set of matched elements or set the HTML contents of every matched element.

Examples

1. Get HTML

// <div class="content"><span>Hi</span></div>
const content = $('.content').html();

Result

"<span>Hi</span>"

2. Set HTML

$('.content').html('<strong>New Content</strong>');

Result

<div class="content">
     <strong>New Content</strong>
</div>

3. Set with Function

$('div').html((index) => `<span>Item ${index}</span>`);

Result

<div><span>Item 0</span></div>
<div><span>Item 1</span></div>

Parameters

Parameter Type Description
content string | function The HTML string to set, or a function that returns an HTML string. Function receives (index, currentHtml).

Returns: The innerHTML string for getter, or the original SR object for setter.