Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy to Clipboard for IE almost working?

Trying get this script to copy to the clipboard and not back into the page. When you click the link it should copy right to the clip board. At least that is my intention. Here are some background facts behinds it:

  • This is for a company intranet site that uses IE exclusivly so it does not need to be compatible with any other browsers
  • The data inside is/will be a return from a db query
  • I realize this is old technology but it needs to be this way for now.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
            <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript"><!--
    // input field descriptions
    var desc = new Array();
            desc['a1'] = 'First name';
    desc['a2'] = 'Last name';
    desc['a3'] = 'Address';
    desc['a4'] = 'Zip';
    desc['a5'] = 'City';
    desc['a6'] = 'Country';
    
    function CopyFields(){
        var copytext = '';
        for(var i = 0; i < arguments.length; i++){
            copytext += desc[arguments[i]] + ': ' + document.getElementById(arguments[i]).innerText + '\n';}
        var tempstore = document.getElementById(arguments[0]).innerText;
                document.getElementById(arguments[0]).innerText = copytext;
        document.getElementById(arguments[0]).focus();
        document.getElementById(arguments[0]).select();
        document.execCommand('Copy');
        document.getElementById(arguments[0]).innerText = tempstore;
    }
    </script>
    </head>
    <body>
    
    <table>
    <tr>
    <td id="a1" name="t1">a</td>
    <td id="a2" name="t2">b</td>
    <td id="a3" name="t3">c</td>
    <td id="a4" name="t4">d</td>
    <td id="a5" name="t5">e</td>
    <td id="a6" name="t6">f</td>
    </tr>
    </table><br>
    <a href="#" onClick="CopyFields('a1', 'a2', 'a3', 'a4', 'a5', 'a6');">Copy values of text fields to clipboard</a>
    </body> 
    
    </html>
    
like image 597
user1839308 Avatar asked Apr 11 '13 20:04

user1839308


People also ask

How do you copy to clipboard?

Open the file that you want to copy items from. Select the first item that you want to copy, and press CTRL+C. Continue copying items from the same or other files until you have collected all of the items that you want. The Office Clipboard can hold up to 24 items.

How do you add text to your clipboard?

Select the text or graphics you want to copy, and press Ctrl+C. Each selection appears in the Clipboard, with the latest at the top. Optionally, repeat step 2 until you've copied all the items you want to use. Tip: After you open the Clipboard, it stores content that you copy or cut from anywhere.

How do you copy text to clipboard on a button click?

Whether it is a sample of code or it is the User's own information, we can create a copy button to copy data to the clipboard using the navigator. clipboard. writeText() function. This function writes the data fed to it as a parameter into the clipboard.


1 Answers

there is no select()-method for td-elements.

You may directly access the clipboard without using the Copy-command:

window.clipboardData.setData('Text', copytext);
like image 195
Dr.Molle Avatar answered Sep 23 '22 09:09

Dr.Molle