About this snippet

Count the characters as your user types. We show a limit indicator that turns red when they type too much, so they know exactly how much space is left in the form field.

Size
1.4 KB
Uses
HTML, CSS, JavaScript
Loads
Nothing external
Views
434
<div class="input-wrapper">
  <label>Tweet / Status</label>
  <textarea id="txtInput" maxlength="280" rows="4" placeholder="What is happening?"></textarea>
  <div class="counter-info">
    <span id="charCount">0</span> / 280
  </div>
</div>
.input-wrapper {
  font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  max-width: 500px;
  margin: 0 auto;
}
label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: #374151;
}
textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  font-size: 1rem;
  resize: vertical;
  box-sizing: border-box;
  outline: none;
  transition: border-color 0.2s;
}
textarea:focus {
  border-color: #3b82f6;
}
textarea.limit-near {
  border-color: #f59e0b;
}
textarea.limit-reached {
  border-color: #ef4444;
}
.counter-info {
  text-align: right;
  font-size: 0.85rem;
  color: #6b7280;
  margin-top: 5px;
}
const input = document.getElementById("txtInput");
const countSpan = document.getElementById("charCount");
const limit = 280;

input.addEventListener("input", function() {
  const currentLength = this.value.length;
  countSpan.innerText = currentLength;
  
  if (currentLength >= limit) {
    this.classList.add("limit-reached");
    countSpan.style.color = "#ef4444";
  } else {
    this.classList.remove("limit-reached");
    countSpan.style.color = "#6b7280";
  }
});
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