40

I want to use a copy-to-clipboard function in HTML5, but without using flash. Is it possible? How?

I tried to implement a copy-to-clipboad function with JavaScript, but it is not working:

function Copytoclipboard() {
    var body = document.body,
        range, sel;
    if (document.createRange && window.getSelection) {
        range = document.createRange();
        sel = window.getSelection();
        sel.removeAllRanges();
        try {
            range.selectNodeContents(el);
            sel.addRange(range);
            document.execCommand('Copy');
        } catch (e) {
            range.selectNode(el);
            sel.addRange(range);
            document.execCommand('Copy');
        }
    } else if (body.createTextRange) {
        range = body.createTextRange();
        range.moveToElementText(el);
        range.select();
        range.execCommand('Copy');
    }
}
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
RKS
  • 591
  • 2
  • 5
  • 6

6 Answers6

28

You can use the HTML5 clipboard api http://www.htmlgoodies.com/html5/other/working-with-clipboard-apis-in-html5-web-apps.html#fbid=eh9tM7GHJWF

But do note that not all browsers fully support it as of now: http://caniuse.com/#feat=clipboard

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Variant
  • 17,279
  • 4
  • 40
  • 65
  • 13
    "Not fully support" is one way to say it. Unusable is another. Chrome, Safari, Opera don't support the EventConstructor which seems essential if you want to copy anything to clipboard. – Martin Thoma Mar 24 '15 at 21:22
  • 3
    But it does answer the question. User asked for html5. – Jomar Sevillejo Aug 05 '15 at 23:23
  • What about support for all the different browsers on mobile devices? There's a million of them. It seems predictions that HTML5 was more about hype and broken promises have come true. If we still can't get this simple micro-feature working consistently in 2015, are we going to all be retired before this stuff "just works"? Maybe plug-in architecture wasn't so bad after all. Works great for Eclipse. – User Nov 14 '15 at 23:50
  • 1
    Love the way that html goodies don't include a working example. 5* – David Mar 15 '16 at 16:26
25

UPDATE: This solution now works in the current version of all major browsers!

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);
})
<input id="txt" />
<button id="btn">Copy To Clipboard</button>

Note: Trying to use this solution to copy an empty string or a string that is only whitespace will not work.

ALTERNATE, SIMPLIFIED SOLUTION

This alternate solution has been tested in Chrome, Safari, and Firefox.

const txt = document.querySelector('#txt')
const btn = document.querySelector('#btn')

const copy = (text) => {
  const textarea = document.createElement('textarea')
  document.body.appendChild(textarea)
  textarea.value = text
  textarea.select()
  document.execCommand('copy')
  textarea.remove()
}

btn.addEventListener('click', (e) => {
  copy(txt.value)
})
<input id="txt" />
<button id="btn">Copy</button>

Note: This solution will not copy an empty string, but it will copy whitespace.

Trevor
  • 13,085
  • 13
  • 76
  • 99
17

It's not working because it requires a user interaction such as click. Otherwise, document.execCommand will not work. You also might wanna check clipboard.js, it's a super easy library to copy text to clipboard that doesn't require Flash.

Zeno Rocha
  • 3,226
  • 1
  • 23
  • 27
15

Function for inserting text into the clipboard:

function copyStringToClipboard (string) {
    function handler (event){
        event.clipboardData.setData('text/plain', string);
        event.preventDefault();
        document.removeEventListener('copy', handler, true);
    }

    document.addEventListener('copy', handler, true);
    document.execCommand('copy');
}
Petr Žoček
  • 151
  • 1
  • 4
  • FYI this will probably work on all today's browsers. For IE would work simple `window.clipboardData.setData("Text", string)`. – Binyamin Aug 13 '17 at 08:08
  • This is the best solution when you want to copy text which is not available in the DOM, e.g. an error message's stack trace. – Tsvetan Ganev Oct 29 '20 at 09:17
  • @Petr Žoček, This sir is a stroke of absolute genius! I would love know where you learned this? It is the only solution I have found after a solid day of searching and trying many solutions and this is the only one that preserves style elements in the data and does not filter them out. You deserve a gold medal! – Bernd Wechner Jul 30 '21 at 12:17
6

If you don't care that the contents of the text field will be selected prior to copy, here is two-line solution that works at least in Chrome 56 and Edge, but I bet it works in other browsers as well.

function clickListener() {
  document.getElementById('password').select();
  document.execCommand('copy');
}

document.getElementById('copy_btn').addEventListener('click', clickListener);
<input id=password value="test">
<button id=copy_btn>Copy</button>

https://jsfiddle.net/uwd0rm08/

Artur INTECH
  • 6,024
  • 2
  • 37
  • 34
0

You can Use Clipboard.js TO add Copy to clipboard. This work without flash take a look on Code Which I use:

//for copy to clickboard
var els = document.querySelectorAll('pre');
for (var i=0; i < els.length; i++) {
//for CLIPBOARD
var atr = els[i].innerHTML;
    els[i].setAttribute("data-clipboard-text", atr);
 //For SELECT
 var ids = "elementID"+[i]
    els[i].setAttribute("id", ids);
    els[i].setAttribute("onclick","selectText(this.id)");
 
}
    var btns = document.querySelectorAll('pre');
    var clipboard = new ClipboardJS(btns);

    clipboard.on('success', function(e) {
        console.log(e);
  });

    clipboard.on('error', function(e) {
        console.log(e);
    });
 
 //for select
 function selectText(id){
    var sel, range;
    var el = document.getElementById(id); //get element id
    if (window.getSelection && document.createRange) { //Browser compatibility
      sel = window.getSelection();
      if(sel.toString() == ''){ //no text selection
         window.setTimeout(function(){
            range = document.createRange(); //range object
            range.selectNodeContents(el); //sets Range
            sel.removeAllRanges(); //remove all ranges from selection
            sel.addRange(range);//add Range to a Selection.
        },1);
      }
    }else if (document.selection) { //older ie
        sel = document.selection.createRange();
        if(sel.text == ''){ //no text selection
            range = document.body.createTextRange();//Creates TextRange object
            range.moveToElementText(el);//sets Range
            range.select(); //make selection.
        }
    }
}
<pre>I Have To Copy it<pre>
<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
To Know More About Its Usage visit Source :html5 copy to clipboard
Ajay Malik
  • 325
  • 4
  • 17