Skip to content

Instantly share code, notes, and snippets.

@frontsideair
Last active February 7, 2017 07:21
Show Gist options
  • Save frontsideair/4e5908c61fa35bcc1437c0d8dc65fc70 to your computer and use it in GitHub Desktop.
Save frontsideair/4e5908c61fa35bcc1437c0d8dc65fc70 to your computer and use it in GitHub Desktop.
Test case for copying test with native APIs. (Works on latest Chrome, Firefox, Safari.)

Test case for copying test with native APIs. (Works on latest Chrome, Firefox, Safari.)

made with esnextbin

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Browser copy</title>
</head>
<body>
<input type="text" id="text" value="hello" />
<button id="copy">Copy</button>
</body>
</html>
document.getElementById('copy').addEventListener('click', e => {
const text = document.getElementById('text')
text.focus()
text.setSelectionRange(0, text.value.length)
// document.execCommand('selectAll')
// this works in safari and chrome, but not in firefox; unless it's a contenteditable
document.execCommand('copy')
text.blur()
})
{
"name": "browser-copy",
"version": "0.0.0"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment