Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="cbwrap">
  <!-- (X) TITLE -->
  <h1 id="cbtitle">Editable Table</h1>

  <!-- (A) PROPER DEMO -->
  <table id="demo" class="editable">
    <!-- (A1) HEADER -->
    <thead>
      <tr>
        <th>Name</th>
        <th>Email</th>
      </tr>
    </thead>

    <!-- (A2) DATA -->
    <tbody>
      <tr>
        <td>Job Doe</td>
        <td>job@doe.com</td>
      </tr>
      <tr>
        <td>Joe Doe</td>
        <td>joe@doe.com</td>
      </tr>
      <tr>
        <td>Joi Doe</td>
        <td>joi@doe.com</td>
      </tr>
      <tr>
        <td>Jon Doe</td>
        <td>jon@doe.com</td>
      </tr>
      <tr>
        <td>Joy Doe</td>
        <td>joy@doe.com</td>
      </tr>
    </tbody>
  </table>
  <div id="cbnotes">
    <div>* Double-click on a cell to edit.</div>
    <div>* Press enter or click outside cell to commit.</div>
    <div>* Press escape to cancel.</div>
  </div>

  <!-- (X) INFO SNIPPET -->
  <div id="cbinfo">
    Visit <a href="https://code-boxx.com/editable-html-table/" target="_blank">Code Boxx</a> for more details.
  </div>
</div>
              
            
!

CSS

              
                /* (A) EDITABLE TABLE */
.editable {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 10px;
}
.editable th, .editable td {
  text-align: left;
  padding: 15px;
}
.editable thead {
  color: #fff;
  background: #8363fd;
}
.editable tbody tr:nth-child(even) {
  background: #dfdfdf;
}
.editable td.edit {
  background: #f8ff88;
}

/* (X) DOES NOT MATTER */
* { box-sizing: border-box; }
body {
  font-family: arial, sans-serif;
  padding: 0; margin: 0; border: 0;
  min-height: 100vh;
  display: flex; justify-content: center; align-items: center;
  background: url(https://images.unsplash.com/photo-1515787366009-7cbdd2dc587b?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2ODkxMjkwOTV8&ixlib=rb-4.0.3&q=85);
  background-size: cover;
  background-position: center;
  backdrop-filter: blur(10px);
}
#cbwrap {
  background: rgba(255, 255, 255, 0.9);
  width: 600px; padding: 20px;
  border-radius: 10px;
}
#cbtitle {
  margin-bottom: 40px;
  text-transform: uppercase;
}
#cbinfo {
  padding: 10px; margin-top: 40px;
  font-weight: 700; text-align: center;
}
#cbinfo a {
  text-decoration: none; padding: 5px;
  color: #fff; background: #a91616;
}
#cbnotes {
  margin-top: 20px;
  font-size: 14px;
}
              
            
!

JS

              
                var editable = {
  // (A) PROPERTIES
  selected : null,  // current selected cell
  value : "", // current selected cell value

  // (B) "CONVERT" TO EDITABLE CELL
  edit : cell => {
    // (B1) REMOVE "DOUBLE CLICK TO EDIT"
    cell.ondblclick = "";

    // (B2) EDITABLE CONTENT
    cell.contentEditable = true;
    cell.focus();

    // (B3) "MARK" CURRENT SELECTED CELL
    cell.classList.add("edit");
    editable.selected = cell;
    editable.value = cell.innerHTML;

    // (B4) PRESS ENTER/ESC OR CLICK OUTSIDE TO END EDIT
    window.addEventListener("click", editable.close);
    cell.onkeydown = evt => {
      if (evt.key=="Enter" || evt.key=="Escape") {
        editable.close(evt.key=="Enter" ? true : false);
        return false;
      }
    };
  },

  // (C) END "EDIT MODE"
  close : evt => { if (evt.target != editable.selected) {
    // (C1) CANCEL - RESTORE PREVIOUS VALUE
    if (evt === false) {
      editable.selected.innerHTML = editable.value;
    }

    // (C2) REMOVE "EDITABLE"
    window.getSelection().removeAllRanges();
    editable.selected.contentEditable = false;

    // (C3) RESTORE CLICK LISTENERS
    window.removeEventListener("click", editable.close);
    let cell = editable.selected;
    cell.onkeydown = "";
    cell.ondblclick = () => editable.edit(cell);

    // (C4) "UNMARK" CURRENT SELECTED CELL
    editable.selected.classList.remove("edit");
    editable.selected = null;
    editable.value = "";

    // (C5) DO WHATEVER YOU NEED
    if (evt !== false) {
      console.log(cell.innerHTML);
      // check value?
      // send value to server?
      // update calculations in table?
    }
  }}
};

// (D) INITIALIZE - DOUBLE CLICK TO EDIT CELL
window.addEventListener("DOMContentLoaded", () => {
  for (let cell of document.querySelectorAll(".editable td")) {
    cell.ondblclick = () => editable.edit(cell);
  }
});
              
            
!
999px

Console