compositionend Commits IME Input

compositionend carries the IME-committed string, so validators can skip compositionupdate drafts and check only final input values.

JavascriptForm Validation

Code

javascriptsnippet
23 lines
// IME composition: the input holds in-progress text until the IME commits
const input = document.querySelector('#name');

input.addEventListener('compositionstart', () => {
  input.dataset.composing = 'true';
  console.log('composing started');
});

input.addEventListener('compositionupdate', (event) => {
  console.log('draft:', event.data);
});

// compositionend fires with the committed string before the final input event
input.addEventListener('compositionend', (event) => {
  input.dataset.composing = 'false';
  console.log('committed:', event.data);
});

input.addEventListener('input', () => {
  // Skip partial IME drafts: only committed text is validated
  if (input.dataset.composing === 'true') return;
  console.log('validated:', input.value.length, 'chars');
});

How it works

Top-level bindings input hold the demo state — nothing sits inside a function scope.

What to watch for

  • Zero-fetch snippet: every identifier resolves inside the language distribution.
  • The snippet leans on document — browser-only surfaces a bare Node process cannot provide.
  • The guard input.dataset.composing === 'true' stops the pass immediately with an early return when its condition holds.

Using it

Output lines are prefixed (composing started, draft:, committed:), so every step’s effect is easy to pick out.

Key statements worth quoting

input.addEventListener('compositionupdate', (event) => {
  console.log('draft:', event.data);
});
input.addEventListener('compositionend', (event) => {
  input.dataset.composing = 'false';
  console.log('committed:', event.data);
});
input.addEventListener('compositionstart', () => {
  input.dataset.composing = 'true';
  console.log('composing started');
});

Interactive Tools for This Topic

FAQ

Does compositionend Commits IME Input run in plain Node.js?
No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.