Skip to content

Instantly share code, notes, and snippets.

@maldechavda
Last active November 14, 2016 09:19
Show Gist options
  • Select an option

  • Save maldechavda/d3c50c39b955329c4457741f097469b6 to your computer and use it in GitHub Desktop.

Select an option

Save maldechavda/d3c50c39b955329c4457741f097469b6 to your computer and use it in GitHub Desktop.
Copy TO Clipboard
<input id="txt" />
<button id="btn">Copy To Clipboard</button>
<script type="text/javascript">
$(document).ready(function() {
function copyText(text){
function selectElementText(element) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(element);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
}
}
var element = document.createElement('DIV');
element.textContent = text;
document.body.appendChild(element);
selectElementText(element);
document.execCommand('copy');
element.remove();
}
var txt = document.getElementById('txt');
var btn = document.getElementById('btn');
btn.addEventListener('click', function(){
copyText(txt.value);
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment