Skip to content

Instantly share code, notes, and snippets.

@makotom
Created November 9, 2019 11:20
Show Gist options
  • Save makotom/664e9a6bb3fe78d93f64f1010a710201 to your computer and use it in GitHub Desktop.
Save makotom/664e9a6bb3fe78d93f64f1010a710201 to your computer and use it in GitHub Desktop.
`change` events on drag-and-drop
<!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#dnd-textbox');
const output = document.querySelector('div#dnd-output');
input.addEventListener('input', () => {
// Forcibly focus on the text box and then blur the focus
// to imitate user commitment.
input.focus();
input.blur();
});
input.addEventListener('change', () => {
// Output its value to a newly-created p element
const p = document.createElement('p');
p.appendChild(document.createTextNode(`The content of the text box is: ${input.value}`));
output.appendChild(p);
});
// Extra for courtesy: Select the reference text upon load
{
const selection = window.getSelection();
selection.removeAllRanges();
selection.selectAllChildren(document.querySelector('p#dnd-desc'));
}
});
</script>
<div id="sandbox-dnd">
<p id="dnd-desc">
With changes on the text box below, a <code>change</code> event fires, and its content appears under the text box.<br>
Then, <strong>what would happen if a user drags and drops this text into the text box?</strong>
</p>
<input id="dnd-textbox">
<div id="dnd-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, change events would not be fired if the content of the text box was updated by drag-and-drop.
On Chrome and Edge, contrarily, change events would 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