Component Library

← Back to gallery

Table cell inline edit in HTML, CSS and vanilla JS

A copy-paste Table cell inline edit in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.

Category: Forms · Tags: inline edit, table, cell, save cancel, form

Live preview

Source (copy-ready)

<section class="example-inline-edit-table-cell" data-dcl-scheme="light" aria-labelledby="example-inline-edit-table-cell-title">
<header><h2 id="example-inline-edit-table-cell-title">Table cell inline edit</h2>
<p>Only the selected table cell becomes a form while the rest of the row remains visible for context.</p></header>
<div class="parts">
<div class="scroll-x">
<table>
  <thead><tr><th scope="col">Component</th><th scope="col">Owner</th><th scope="col">State</th></tr></thead>
  <tbody>
    <tr>
      <td>Checkout form</td>
      <td data-cell>
        <div class="cell-view" data-view><span data-value>Priya Shah</span> <button type="button" data-edit>Edit</button></div>
        <form class="cell-form" data-form hidden novalidate>
          <label class="visually-hidden" for="example-inline-edit-table-cell-input">Owner for Checkout form</label>
          <input type="text" id="example-inline-edit-table-cell-input" value="Priya Shah" required data-input>
          <span class="actions"><button type="submit">Save</button><button type="button" data-cancel>Cancel</button></span>
        </form>
      </td>
      <td>Live</td>
    </tr>
    <tr><td>Invoice email</td><td>Mina Lee</td><td>Draft</td></tr>
  </tbody>
</table>
</div>
<p class="status" role="status" aria-live="polite" data-status></p>
</div>
<script>
(() => {
  const root = document.currentScript.closest(".example-inline-edit-table-cell");
  const instance = Array.from(document.getElementsByClassName("example-inline-edit-table-cell")).indexOf(root);
  if (instance > 0) {
    const singleReferences = ["id", "aria-activedescendant", "for", "list", "name"];
    const tokenReferences = ["aria-labelledby", "aria-controls", "aria-describedby"];
    for (const element of [root, ...root.querySelectorAll("*")]) {
      for (const attr of singleReferences) {
        if (element.hasAttribute(attr)) element.setAttribute(attr, element.getAttribute(attr) + "--" + instance);
      }
      for (const attr of tokenReferences) {
        if (element.hasAttribute(attr)) {
          const rewritten = element.getAttribute(attr).trim().split(/\s+/).filter(Boolean).map((reference) => reference + "--" + instance);
          element.setAttribute(attr, rewritten.join(" "));
        }
      }
      const href = element.getAttribute("href");
      if (href && href.startsWith("#")) element.setAttribute("href", href + "--" + instance);
    }
  }
  const preference = matchMedia("(prefers-color-scheme: dark)");
  const apply = () => { root.dataset.dclScheme = preference.matches ? "dark" : "light"; };
  preference.addEventListener("change", apply);
  apply();
  const view = root.querySelector("[data-view]");
  const form = root.querySelector("[data-form]");
  const edit = root.querySelector("[data-edit]");
  const input = root.querySelector("[data-input]");
  const cancel = root.querySelector("[data-cancel]");
  const value = root.querySelector("[data-value]");
  const status = root.querySelector("[data-status]");
  edit.addEventListener("click", () => {
    input.value = value.textContent;
    view.hidden = true;
    form.hidden = false;
    input.focus();
    input.select();
  });
  function close(message) {
    form.hidden = true;
    view.hidden = false;
    status.textContent = message;
    edit.focus();
  }
  cancel.addEventListener("click", () => close("Cell edit cancelled."));
  function submitForm() {
    if (!input.checkValidity()) {
      input.reportValidity();
      input.focus();
      return;
    }
    value.textContent = input.value.trim();
    close("Owner updated.");
  }
  form.addEventListener("submit", (event) => {
    event.preventDefault();
    submitForm();
  });
  const submitButton = form.querySelector('button[type="submit"]');
  if (submitButton) {
    submitButton.addEventListener("click", (event) => {
      event.preventDefault();
      submitForm();
    });
  }
  form.addEventListener("keydown", (event) => {
    if (event.key !== "Enter" || event.defaultPrevented) return;
    if (!event.target.matches("input, select")) return;
    event.preventDefault();
    submitForm();
  });
})();
</script></section>
<style>
.example-inline-edit-table-cell { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-inline-edit-table-cell a { color: #005c62; }
.example-inline-edit-table-cell button, .example-inline-edit-table-cell summary, .example-inline-edit-table-cell input, .example-inline-edit-table-cell select, .example-inline-edit-table-cell textarea {
  color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-inline-edit-table-cell button:hover, .example-inline-edit-table-cell summary:hover, .example-inline-edit-table-cell button[aria-selected="true"], .example-inline-edit-table-cell button[aria-pressed="true"] {
  color: #ffffff; background-color: #00535a;
}
.example-inline-edit-table-cell dialog { color: #11292f; background-color: #ffffff; }
.example-inline-edit-table-cell [data-note] { color: #11292f; background-color: #e4edef; }
.example-inline-edit-table-cell [data-error] { color: #7f1d1d; background-color: #fee2e2; }
.example-inline-edit-table-cell [data-success] { color: #14532d; background-color: #dcfce7; }
.example-inline-edit-table-cell [data-warning] { color: #713f12; background-color: #fef9c3; }
.example-inline-edit-table-cell :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }

.example-inline-edit-table-cell[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; }
.example-inline-edit-table-cell[data-dcl-scheme="dark"] a { color: #8fe1da; }
.example-inline-edit-table-cell[data-dcl-scheme="dark"] button, .example-inline-edit-table-cell[data-dcl-scheme="dark"] summary,
.example-inline-edit-table-cell[data-dcl-scheme="dark"] input, .example-inline-edit-table-cell[data-dcl-scheme="dark"] select, .example-inline-edit-table-cell[data-dcl-scheme="dark"] textarea {
  color: #eff8f9; background-color: #1c3c44; border-color: #839fa5;
}
.example-inline-edit-table-cell[data-dcl-scheme="dark"] button:hover, .example-inline-edit-table-cell[data-dcl-scheme="dark"] summary:hover,
.example-inline-edit-table-cell[data-dcl-scheme="dark"] button[aria-selected="true"], .example-inline-edit-table-cell[data-dcl-scheme="dark"] button[aria-pressed="true"] {
  color: #11292f; background-color: #8fe1da;
}
.example-inline-edit-table-cell[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #142f36; }
.example-inline-edit-table-cell[data-dcl-scheme="dark"] [data-note] { color: #eff8f9; background-color: #1c3c44; }
.example-inline-edit-table-cell[data-dcl-scheme="dark"] [data-error] { color: #fecaca; background-color: #451a1a; }
.example-inline-edit-table-cell[data-dcl-scheme="dark"] [data-success] { color: #bbf7d0; background-color: #14532d; }
.example-inline-edit-table-cell[data-dcl-scheme="dark"] [data-warning] { color: #fde68a; background-color: #451a03; }
.example-inline-edit-table-cell[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }

.example-inline-edit-table-cell, .example-inline-edit-table-cell * { box-sizing: border-box; min-width: 0; }
.example-inline-edit-table-cell { display: block; font: 1rem/1.5 system-ui, sans-serif; padding: 1rem; border-radius: 0.75rem; overflow-wrap: anywhere; }
.example-inline-edit-table-cell h2, .example-inline-edit-table-cell h3 { line-height: 1.15; margin-block: 0 0.75rem; }
.example-inline-edit-table-cell header p, .example-inline-edit-table-cell p { margin-block: 0.5rem; }
.example-inline-edit-table-cell ul, .example-inline-edit-table-cell ol { margin-block: 0.5rem; padding-inline-start: 1.5rem; }
.example-inline-edit-table-cell dl { margin-block: 0.5rem; }
.example-inline-edit-table-cell dt { font-weight: 700; }
.example-inline-edit-table-cell dd { margin: 0 0 0.6rem; }
.example-inline-edit-table-cell fieldset { min-inline-size: 0; margin: 0; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-inline-edit-table-cell legend { padding-inline: 0.25rem; font-weight: 700; }
.example-inline-edit-table-cell button, .example-inline-edit-table-cell input, .example-inline-edit-table-cell select, .example-inline-edit-table-cell textarea {
  min-block-size: 44px; border: 1px solid; border-radius: 0.375rem; padding: 0.5rem 0.75rem; font: inherit;
}
.example-inline-edit-table-cell input, .example-inline-edit-table-cell select, .example-inline-edit-table-cell textarea { display: block; inline-size: 100%; max-inline-size: 100%; }
.example-inline-edit-table-cell input[type="checkbox"], .example-inline-edit-table-cell input[type="radio"] {
  display: inline-block; inline-size: auto; min-block-size: auto; padding: 0; flex: 0 0 auto;
}
.example-inline-edit-table-cell input[type="file"] { padding: 0.45rem; }
.example-inline-edit-table-cell input[type="range"] { padding-inline: 0; }
.example-inline-edit-table-cell textarea { min-block-size: 6rem; resize: vertical; }
.example-inline-edit-table-cell button, .example-inline-edit-table-cell summary { cursor: pointer; transition: background-color 140ms ease-out; }
.example-inline-edit-table-cell button:disabled { cursor: not-allowed; text-decoration: line-through; }
.example-inline-edit-table-cell table { border-collapse: collapse; inline-size: 100%; }
.example-inline-edit-table-cell th, .example-inline-edit-table-cell td { padding: 0.5rem; text-align: start; border-block-end: 1px solid; }
.example-inline-edit-table-cell [hidden] { display: none !important; }
.example-inline-edit-table-cell [data-note], .example-inline-edit-table-cell [data-error], .example-inline-edit-table-cell [data-success], .example-inline-edit-table-cell [data-warning] { border-radius: 0.5rem; padding: 0.65rem; }
.example-inline-edit-table-cell .parts { display: grid; gap: 1rem; }
.example-inline-edit-table-cell .field, .example-inline-edit-table-cell .fields { display: grid; gap: 0.4rem; }
.example-inline-edit-table-cell .fields { gap: 0.75rem; }
.example-inline-edit-table-cell .actions, .example-inline-edit-table-cell .inline-row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.example-inline-edit-table-cell .panel { border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-inline-edit-table-cell .plain-list { margin: 0; padding: 0; list-style: none; }
.example-inline-edit-table-cell .choice { display: flex; gap: 0.5rem; align-items: flex-start; }
.example-inline-edit-table-cell .choice input { margin-block-start: 0.25rem; }
.example-inline-edit-table-cell .choice-stack { display: grid; gap: 0.55rem; }
.example-inline-edit-table-cell .tile { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 0.55rem; align-items: start; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-inline-edit-table-cell .tile[data-active] { border-width: 3px; padding: calc(0.75rem - 2px); }
.example-inline-edit-table-cell .pill { display: inline-flex; gap: 0.4rem; align-items: center; border: 1px solid; border-radius: 999px; padding: 0.35rem 0.7rem; }
.example-inline-edit-table-cell .pill[data-active] { border-width: 3px; padding: calc(0.35rem - 2px) calc(0.7rem - 2px); }
.example-inline-edit-table-cell .hint { font-size: 0.875rem; }
.example-inline-edit-table-cell .status { min-block-size: 1.5rem; margin-block: 0; }
.example-inline-edit-table-cell .two-column, .example-inline-edit-table-cell .split-layout { display: grid; gap: 1rem; }
.example-inline-edit-table-cell .sticky-panel { position: sticky; inset-block-start: 1rem; align-self: start; }
.example-inline-edit-table-cell .scroll-x { overflow-x: auto; max-inline-size: 100%; }
.example-inline-edit-table-cell .visually-hidden {
  position: absolute; inline-size: 1px; block-size: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}
@media (min-width: 34rem) {
  .example-inline-edit-table-cell .two-column { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .example-inline-edit-table-cell .split-layout { grid-template-columns: minmax(0, 1fr) minmax(12rem, 18rem); }
}
@media (max-width: 30rem) {
  .example-inline-edit-table-cell { padding: 0.75rem; }
  .example-inline-edit-table-cell .actions, .example-inline-edit-table-cell .inline-row { align-items: stretch; }
  .example-inline-edit-table-cell .wizard-steps { grid-template-columns: minmax(0, 1fr) !important; }
}
@media (prefers-reduced-motion: reduce) { .example-inline-edit-table-cell * { transition: none !important; } }
.example-inline-edit-table-cell .cell-form[hidden] { display: none !important; }
.example-inline-edit-table-cell .cell-view[hidden] { display: none !important; }

.example-inline-edit-table-cell .cell-view { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.example-inline-edit-table-cell .cell-form { display: grid; gap: 0.5rem; min-inline-size: 12rem; }
.example-inline-edit-table-cell .cell-form .actions { display: flex; gap: 0.4rem; flex-wrap: wrap; }
</style>

When to use

Use this when tabular data needs occasional correction without opening a row-wide editor. Keep the edit inside the cell so the column headers still explain the value.

Avoid

Do not switch an entire table row into inputs for a single-cell correction; it disrupts scanning and comparison.

Accessibility

The table keeps real header cells, editing starts from a native button, focus moves to a labelled input, and Save/Cancel are explicit. Confirmation is announced without changing the table structure. Main text contrast is 15.19:1 light and 13.07:1 dark; controls are 12.78:1 and 10.94:1.

Was this useful? Send feedback.