What Each Does:
change Event:
- Fires when user finishes changing a value (not while typing)
- Text inputs: fires on blur (when you click away)
- Dropdowns/checkboxes: fires immediately when selection changes
input Event:
- Fires every single time the value changes (while typing, pasting, everything)
- Text inputs: fires on every keystroke, paste, voice input, etc.
- Most sensitive to changes
cut/copy/paste Events:
- Fire when user tries to cut/copy/paste
- Can be prevented with
preventDefault() - Give access to clipboard data
The Mechanics:
javascript
// change - fires when done editing (blur for text inputs)
input.onchange = function() {
console.log("Final value:", this.value); // Only when you click away
};
// input - fires on every modification
input.oninput = function() {
console.log("Current value:", this.value); // Every keystroke
};
// Clipboard events
input.onpaste = function(e) {
console.log("Pasted:", e.clipboardData.getData('text/plain'));
e.preventDefault(); // Stop the paste
};Real Behavior:
- change: Type “hello” → nothing → click elsewhere → change fires once
- input: Type “hello” → input fires 5 times (h, e, l, l, o)
- paste: Right-click paste → paste event → can block or read clipboard data
Interview Gotchas:
-
Can’t prevent input event:
javascript
input.oninput = function(e) { e.preventDefault(); // DOESN'T WORK - change already happened }; -
Clipboard data timing:
javascript
input.oncopy = function(e) { console.log(e.clipboardData.getData('text')); // EMPTY - not copied yet console.log(document.getSelection()); // Use this instead }; -
Change vs Input for validation:
javascript
// Real-time validation (as user types) input.oninput = validateField; // Final validation (when done editing) input.onchange = saveToServer;
Bottom Line:
input= every tiny change (live updates)change= when user is done changing (final validation)- Clipboard events = can intercept and block copy/paste operations
