Skip to content

Instantly share code, notes, and snippets.

@makotom
Created November 9, 2019 11:25
Show Gist options
  • Save makotom/8efeec1e34532e2ec67ccaeeb8ad6198 to your computer and use it in GitHub Desktop.
Save makotom/8efeec1e34532e2ec67ccaeeb8ad6198 to your computer and use it in GitHub Desktop.
`change` events on manipulated text input
<!doctype html>
<html lang="en">
<meta charset="UTF-8">
<title>Change events on text box</title>
<script>
window.addEventListener('DOMContentLoaded', () => {
const input = document.querySelector('input#autoincr-textbox');
const output = document.querySelector('div#autoincr-output');
input.addEventListener('change', () => {
// Output its value to a newly-created p element
const p = document.createElement('p');
p.appendChild(document.createTextNode(`At ${Date.now()}: A change was made.`));
output.appendChild(p);
});
// Refresh the content of the text box once per 2 seconds.
{
let ctr = 0;
const incr = () => {
input.value = `${ctr}`;
// Blur focus to mimic a user commitment
input.blur();
ctr = (ctr + 1) % 10;
};
incr();
window.setInterval(incr, 2000);
}
});
</script>
<div id="sandbox-autoincr">
<p id="autoincr-desc">
<code>change</code> events should not be fired unless a user commits the change<sup><a href="https://html.spec.whatwg.org/multipage/indices.html#event-change">&dagger;</a></sup>.<br>
Then, what would happen if a computer manipulates user input and then mimics a user commitment? Input a text onto the text box below <strong>without commiting your input (i.e. without hitting Enter key or Tab key)</strong>, and see whether a message saying "a change was made" appears afterwards.
</p>
<input id="autoincr-textbox">
<div id="autoincr-output"></div>
</div>
@makotom
Copy link
Author

makotom commented Nov 9, 2019

Open the HTML file with your browser and follow the instruction.

For instance:

On Firefox and Chrome, change events would be fired upon blur(), regardless whether the effective value at the event is originated from user input, if a user has ever manipulated its contents before the blur.
On Edge, change events would not be fired.

Confirmed with:
Windows 10 1903 (18362.449)

Firefox 70.0.1
Chrome 78.0.3904.97
Edge 18.18362

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment