<div class="container">
  <h1>More Fun With execCommand</h1>
  <p>Try it out by selecting some text in the paragraph below and the buttons:</p>
  
  <p contenteditable="true" spellcheck="false" class="box">
     Phasellus semper magna ac interdum scelerisque. Vestibulum laoreet sapien a nisi ullamcorper, nec luctus neque eleifend. Aliquam vel tortor justo. Nam aliquam dapibus mi ut volutpat. Donec eros turpis, gravida et neque nec, interdum pulvinar nibh. Ut vel sodales nulla, a tempus sem. Nunc facilisis pretium aliquam. Proin nulla ante, congue at purus sit amet, commodo placerat erat. Sed malesuada eros nulla, in cursus urna elementum finibus. Suspendisse posuere purus pellentesque rutrum tempor. Nunc nec tincidunt est. Sed convallis ligula quis dui laoreet maximus. Nulla pharetra placerat quam ut rutrum. Nullam sit amet vehicula nisi.
  </p>
  
  <p class="text-center">
    <button id="bold-btn">Bold</button>
    <button id="italic-btn">Italic</button>
    <button id="underline-btn">Underline</button>
    <button id="strike-through-btn">StrikeThrough</button>
  </p>

  <p class="text-center">
    <button id="supscript-btn">Supscript</button>
    <button id="superscript-btn">Superscript</button>
  </p>
  
   <p class="text-center">
    <button id="color-btn">Color</button>
    <button id="size-btn">Size</button>
    <button id="delete-btn">Delete</button>
    <button id="undo-btn">Undo</button>
    <button id="redo-btn">Redo</button>
  </p>
</div>
.box {
  border: 1px solid #aaa;
  border-radius: 4px;
  padding: 15px;
}

.text-center {
  text-align: center;
}
document.addEventListener("DOMContentLoaded", () => {
  const $ = document.querySelector.bind(document);
  const boldBtn = $("#bold-btn"),
        italicBtn = $("#italic-btn"),
        underlineBtn = $("#underline-btn"),
        strikeThroughBtn = $("#strike-through-btn"),
        subscriptBtn = $("#supscript-btn"),
        superscriptBtn = $("#superscript-btn"),
        colorBtn = $("#color-btn"),
        sizeBtn = $("#size-btn"),
        deleteBtn = $("#delete-btn"),
        undoBtn = $("#undo-btn"),
        redoBtn = $("#redo-btn");
  
  boldBtn.addEventListener("click", () => {
    document.execCommand('bold');
  });
  
  italicBtn.addEventListener("click", () => {
    document.execCommand('italic');
  });
  
  underlineBtn.addEventListener("click", () => {
    document.execCommand('underline');
  });
  
  strikeThroughBtn.addEventListener("click", () => {
    document.execCommand('strikeThrough');
  });
  
  subscriptBtn.addEventListener("click", () => {
    document.execCommand('subscript');
  });
  
  superscriptBtn.addEventListener("click", () => {
    document.execCommand('superscript');
  });
  
  colorBtn.addEventListener("click", () => {
    document.execCommand('styleWithCSS', false, true);
    document.execCommand('foreColor', false, 'hotpink');
  });
  
  sizeBtn.addEventListener("click", () => {
    document.execCommand('styleWithCSS', false, true);
    document.execCommand('fontSize', false, '25px');
  });
  
  deleteBtn.addEventListener("click", () => {
    document.execCommand('delete');
  });
  
  undoBtn.addEventListener("click", () => {
    document.execCommand('undo');
  });
  
  redoBtn.addEventListener("click", () => {
    document.execCommand('redo');
  });
});

// https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand

External CSS

  1. https://codepen.io/completejavascript/pen/RqpPye.css

External JavaScript

This Pen doesn't use any external JavaScript resources.