← Back to Index

.html()

Get the HTML contents of the first element in the set of matched elements or set the 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 String

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

Result

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

3. Set with Element or Object

You can pass a DOM element or SR object. The container will be emptied, and the element will be appended.

const $iframe = $.t('<iframe>', { src: '/page' });
$('.frame-container').html($iframe);

Result

<div class="frame-container">
     <iframe src="/page"></iframe>
</div>

4. 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 | element | SR | function The content to set. Can be an HTML string, a DOM element, an SR object, or a function returning any of these. Function receives (index, currentHtml).

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