Example 1 — Bold a Selection
Focus a contenteditable region, select all, then run bold.
const editor = document.getElementById("editor"); // contenteditable
editor.focus();
document.execCommand("selectAll", false, null);
const ok = document.execCommand("bold", false, null);
console.log(ok); // true or false
console.log(editor.innerHTML); // often wraps text in <b> or <strong> How It Works
- Focus. Commands apply to the active editable element.
- Select.
boldformats the current selection. - Run the command. Second argument is usually
false. - Check
ok.falsemeans unsupported or blocked.

