Exec editor

Here’s a couple things worth knowing when you want to receive user input and rapidly display the text with a few styling options:

  1. contentEditable set to true is enough to enable rich editing with keyboard shortcuts

    <div contenteditable="true"></div>

    The options are limited as you can increase only increase the font weight — Ctrl + B, incline the letters — Ctrl + I, or toggle a neat underline — Ctrl + U.

    Even worse, the “feature” works only on Chromium-based browsers. Test the code on FireFox and you are stuck with unstyled text.

  2. if you want to keep a consistent interaction and avoid the changes contentEditable supports another value with the simpler plaintext-only

    <div contenteditable="plaintext-only"></div>
  3. to expand the features to more web browser you can try the execCommand method on the document object

    document.execCommand("bold", false, undefined);

    The function is noted as deprecated, but there aren’t quick alternatives to its features.

When you have the time for a more serious piece of editing software you might consider a dedicated library, or accept to re-implement the execCommand niceties all on your own. But if you still rely on the still widespread API, know that when you want to signal that a flag is active you don’t need to keep track of state with a variable you update next to the call to the express function. To keep a marker in sync, and since you are already relying on a deprecated method, you can query the state understanding the warning coming with queryCommandState.

document.queryCommandState("bold"); // true