jQuery provides simple and powerful methods to get and set HTML content inside elements. The most commonly used method is .html().
.html() retrieves or updates HTML markup.text() works with plain text onlyCalling .html() without arguments returns the inner HTML of the selected element. Passing a string replaces the existing content.
// Get HTML content of an element
$("#example").html();
// Set new HTML content inside an element
$("#example").html("<b>Bold text</b>");
Click the buttons to see how HTML content is fetched and replaced dynamically.
// Compare html() and text() methods
let htmlContent = $("#example").html();
let textContent = $("#example").text();
Type some HTML code below (e.g., <button>Click me</button>):
.html() only with trusted content.text() for user input.append() to add content instead of replacing.html() vs .text() outputs