This is a fairly late answer, but for those looking in the future, and has problems implementing the execCommand ('copy') event to work on both desktop and mobile devices.
Cross browser, convenient for mobile devices and no need for external sources or programs
function CopyString(){ var StringToCopyElement = document.getElementById('YourId'); StringToCopyElement.select(); if(document.execCommand('copy')){ StringToCopyElement.blur(); }else{ CopyStringMobile(); } } function CopyStringMobile(){ document.getElementById("YourId").selectionStart = 0; document.getElementById("YourId").selectionEnd = 999; document.execCommand('copy'); if (window.getSelection) { if (window.getSelection().empty) { // Chrome window.getSelection().empty(); } else if (window.getSelection().removeAllRanges) { // Firefox window.getSelection().removeAllRanges(); } } else if (document.selection) { // IE? document.selection.empty(); } }
Set the CopyString () function to the click event on everything you want to fire the event. It is available for use on both mobile and desktop operating systems.
Explanation
You need to have two different ways to choose the line to copy, because today the method for this through the desktop will not work for mobile devices. I have an if then function to catch if the desktop worked, and if not, to run code that will work for a mobile device. This method does not require downloads or links. Both methods highlight the text you want to copy, then run the copy command on the clipboard, and then delete the line you are trying to copy. You can mix the code to your liking, but this is the way to do it.
MattOlivos Apr 19 '17 at 3:25 2017-04-19 03:25
source share