jQuery .html() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

.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.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
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>";
});
CallWhat it does
.html()Learn jQuery .html(): get or set inner HTML, first-match getter, replace children when setting, callback since 1.4, html vs text vs append, XSS safety, examples, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select the element(s).
Step 2Call .html() with no args to read the first match.
Step 3Pass an HTML string to replace all children of every match.

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Get HTML

Read the inner markup of the first matched element.

jQuery
var markup = $("#box").html();
// e.g. "Hello <em>world</em>"
Try It Yourself

How It Works

  1. Select. $("#box") finds the target.
  2. Get. No argument → read innerHTML.
  3. First only. Extra matches are ignored by the getter.

Example 2 — Set HTML

Replace everything inside each matched element.

jQuery
$("div").html("<strong>Hello Again</strong>");
Try It Yourself

How It Works

  1. Select. Every div is matched.
  2. Clean. Old children lose jQuery data/handlers, then are removed.
  3. Parse. The string becomes DOM nodes via innerHTML.

Example 3 — .html() vs .text()

Read markup, then turn it into visible text (tags shown as characters).

jQuery
$("p").on("click", function () {
  var htmlString = $(this).html();
  $(this).text(htmlString);
});
Try It Yourself

How It Works

  1. Get. .html() returns markup including tags.
  2. Set text. .text(htmlString) inserts that string as plain text.
  3. Result. Users see <b>...</b> characters, not bold formatting.

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.

Next: prependTo() Method — Insert Into Target First

Insert matched elements at the start of a target container.

prependTo() Method — Insert Into Target First tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful