.html() gets or sets an element’s HTML contents via innerHTML (since jQuery 1.0). No args → read the first match. With a string → replace children of every match. Not available on XML documents.
Remember
var markup = $("#box").html(); // get (first match)
$("#box").html("<strong>Hi</strong>"); // set (replace children)
$("div").html(function (i, old) { // callback since 1.4
return old + " <em>#" + i + "</em>";
});
Decide whether you are reading or replacing inner markup.
Select the element(s).
Call .html() with no args to read the first match.
Pass an HTML string to replace all children of every match.
Or pass a function (index, oldhtml) since 1.4 to build new markup per element.
Piece
Required?
Role
Matched set
Yes
Element(s) to read or update
htmlString
For setter
Trusted markup that replaces children
function
Optional (1.4+)
Returns new HTML; gets index, oldhtml
Pseudocode
Pseudocode
if no argument:
return innerHTML of first matched element
// (undefined if empty set)
if argument is a function:
for each matched element el at index i:
old = el.innerHTML
empty el (clear children + clean data/handlers)
newHtml = function.call(el, i, old)
set el.innerHTML = newHtml
else:
for each matched element el:
replace all children with parsed htmlString
// child data/handlers cleaned first
return original matched set (setter)
Reference
Cheat Sheet
Keep these facts handy while editing markup.
Topic
Detail
Get
.html() → first match only
Set
.html(htmlString) → every match
Callback
.html(fn) since 1.4
Since
jQuery 1.0
Engine
Browser innerHTML
XML
Not supported
Security
Untrusted strings → XSS risk
Try it
Live Preview
Get, set, or clear the sample box’s HTML.
Browser demo of .html() get / set — same idea as the examples below.
Hello world
Result
$("#box").html()
→ Hello <em>world</em>
Code
jQuery Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — Get HTML
Read the inner markup of the first matched element.
jQuery
var markup = $("#box").html();
// e.g. "Hello <em>world</em>"
Scripts and event attributes in HTML strings can run. Sanitize, escape, or use .text().
getter
Getter is first-match only
$("div").html() never concatenates every div — only the first.
callback
Element is emptied before the callback
Rely on oldhtml, not a nested .html() read inside the function.
form
Textareas need .text() / .val()
Official guidance: a <textarea> does not contain HTML — do not use .html() for its value.
Compatibility
Browser Support
.html() has been part of jQuery since 1.0. The function callback arrived in 1.4. It uses the browser’s innerHTML and works wherever your loaded jQuery version runs (HTML documents only).
✓ Stable · jQuery 1.0+
jQuery .html()
Supported in all browsers that run jQuery 1.0+. Prefer a current jQuery 3.x build for modern projects.
100%jQuery core method
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.html()Universal
Bottom line: Safe for everyday UI with trusted markup. Never feed untrusted strings into .html().
Analysis
Performance Notes
Approach
Cost
Best for
.html() get
Cheap
Reading one container
.html(small)
Moderate
Swapping panel markup
Huge HTML strings often
Expensive
Avoid — update smaller pieces
Setting HTML rebuilds the subtree. For frequent small changes, prefer .text(), .append(), or updating one node.
Remember
Key Takeaways
Get: first match only; returns an HTML string.
Set: replaces all children of every match.
Safety: untrusted input → use .text() or sanitize.
Vs append: html replaces; append adds.
One line:.html() is jQuery’s get/set for innerHTML — powerful for trusted markup, dangerous for untrusted strings.
Frequently Asked Questions
With no arguments, .html() returns the HTML contents (innerHTML) of the first matched element. With an argument, it replaces all children inside every matched element. Available since jQuery 1.0. Not available on XML documents.
.html() reads or writes markup — tags are parsed when setting. .text() reads or writes plain text only. Prefer .text() for untrusted or plain strings; use .html() only with trusted markup.
.html() replaces all existing inner content. .append() adds content as the last child and keeps what was already there.
No. The getter returns only the first element's innerHTML. Use .each() if you need every element's HTML.
Yes, since jQuery 1.4: .html(function(index, oldhtml)). jQuery empties the element before the function runs; use oldhtml for the previous markup. Return the new HTML string.
No. Setting HTML from untrusted sources can cause XSS via script tags or event attributes. Escape or sanitize first, or use .text() instead.
Both clear contents. Prefer .empty() when your intent is “clear this container.” Prefer .html(string) when you are replacing markup in one step.