About this snippet

Click any text to edit it instantly. This interface switches between a text display and an input field, so users can update their profile or settings quickly without reloading the page.

Size
2.1 KB
Uses
HTML, CSS, JavaScript
Loads
Nothing external
Views
342
<div class="editable-wrapper">
  <label>Display Name</label>
  <div class="editable-field" id="editContainer">
    <span class="text-value" id="textValue">John Doe</span>
    <input type="text" class="edit-input" id="textInput" value="John Doe">
    <button class="icon-btn" id="editBtn">&#9998;</button>
    <button class="icon-btn save-btn" id="saveBtn">&#10003;</button>
  </div>
</div>
.editable-wrapper {
  font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  max-width: 300px;
  margin: 0 auto;
}
label {
  display: block;
  font-size: 0.85rem;
  color: #6b7280;
  margin-bottom: 5px;
}
.editable-field {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border: 1px solid transparent;
  border-radius: 8px;
  transition: background 0.2s;
}
.editable-field:hover {
  background: #f9fafb;
}
.text-value {
  font-weight: 600;
  color: #1f2937;
  flex: 1;
}
.edit-input {
  display: none;
  flex: 1;
  padding: 6px;
  border: 1px solid #3b82f6;
  border-radius: 4px;
  font-size: 1rem;
  outline: none;
}
.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: #9ca3af;
  font-size: 1.1rem;
}
.icon-btn:hover {
  color: #3b82f6;
}
.save-btn {
  display: none;
  color: #10b981;
}
/* Editing State */
.editable-field.editing .text-value,
.editable-field.editing #editBtn {
  display: none;
}
.editable-field.editing .edit-input,
.editable-field.editing .save-btn {
  display: block;
}
const container = document.getElementById("editContainer");
const textValue = document.getElementById("textValue");
const textInput = document.getElementById("textInput");
const editBtn = document.getElementById("editBtn");
const saveBtn = document.getElementById("saveBtn");

editBtn.addEventListener("click", () => {
  container.classList.add("editing");
  textInput.focus();
});

saveBtn.addEventListener("click", () => {
  textValue.innerText = textInput.value;
  container.classList.remove("editing");
});

// Save on Enter key
textInput.addEventListener("keypress", (e) => {
  if (e.key === "Enter") saveBtn.click();
});
Esc
↑↓ moveEnter select

Start from a template

Open an .html file, or HTML, CSS, JS, Markdown, Less or TypeScript files. You can also drop them here. Styles and scripts go into their panes.

Share

Source

Keyboard shortcuts

Save