javascript 27 lines · 7 steps

A live character counter for textareas

Attach an accessible, self-updating character count to any textarea and hand back a cleanup function.

Explained by highlit
1function initCharacterCounter(textarea, options = {}) {
2 const maxLength = options.maxLength ?? 280;
3 const warnThreshold = options.warnThreshold ?? 0.9;
4 
5 const counter = document.createElement('div');
6 counter.className = 'char-counter';
7 counter.setAttribute('aria-live', 'polite');
8 textarea.insertAdjacentElement('afterend', counter);
9 
10 const update = () => {
11 const length = [...textarea.value].length;
12 const remaining = maxLength - length;
13 
14 counter.textContent = `${remaining} character${remaining === 1 ? '' : 's'} remaining`;
15 counter.classList.toggle('char-counter--warning', length >= maxLength * warnThreshold);
16 counter.classList.toggle('char-counter--error', remaining < 0);
17 textarea.setAttribute('aria-invalid', String(remaining < 0));
18 };
19 
20 textarea.addEventListener('input', update);
21 update();
22 
23 return () => {
24 textarea.removeEventListener('input', update);
25 counter.remove();
26 };
27}
01 / 01
STEP 01

Walkthrough

Space play ←→ step click any line
Three takeaways
  1. 1Returning a teardown function lets callers undo every side effect the setup created.
  2. 2aria-live and aria-invalid keep dynamic UI changes legible to screen readers.
  3. 3Spreading a string into an array counts Unicode code points rather than UTF-16 units.

Related explainers

Share this explainer

Here's the card — post it anywhere.

A live character counter for textareas — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code