Results 1 to 3 of 3

Thread: Copy text from form to clipboard.

  1. #1
    Join Date
    Feb 2020
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question Copy text from form to clipboard.

    I found " A JavaScript scientific calculator." at Javascript Kits http://www.javascriptkit.com/script/cut42.shtml and would like to add a button to copy the results to the clipboard. I have no problem adding the button but writing the copy2clip routine has me lost as the calculator doesn't use the DOM but gets the info from a form.

    I appreciate your help.

    RONC

  2. #2
    Join Date
    Mar 2011
    Posts
    2,009
    Thanks
    59
    Thanked 107 Times in 105 Posts
    Blog Entries
    4

    Default

    Add a javascript function
    Code:
    function copyToClip(form)
    {
    	form.display.select();
    	form.display.setSelectionRange(0, 99999);
    	document.execCommand("copy");
    }
    Add a new button
    HTML Code:
    <TD ALIGN="center"><INPUT TYPE="button" VALUE="copy" NAME="copy" ONCLICK="copyToClip(this.form)"></TD>

    Please note that this calculator script is quite old and outdated. I'm not sure if there's a newer version available somewhere.

  3. #3
    Join Date
    Feb 2020
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Thanks for the response.

    For the function, I found this and it works.

    JS FUNCTION:
    Code:
            function copy2Clipboard(input)
            {
                try
                {
                    navigator.clipboard.writeText(input.value); // Copy text
                }
                catch (err) // Catch errors
                {
                    alert("Failed to copy: " + err);
                }
            }
    HTML:
    Code:
    <TD ALIGN="center"><INPUT TYPE="button" VALUE="copy" NAME="copy" ONCLICK="copy2Clipboard(this.form)"></TD>
    I couldn't get your version to work. This one worked in Chrome and Firefox on win10.

    I haven't seen a newer version of the calculator code.

    RONC

  4. The Following User Says Thank You to rechmbrs For This Useful Post:

    almuliki (02-28-2020)

Similar Threads

  1. clipboard script to copy the form contents
    By sadakathullah in forum JavaScript
    Replies: 2
    Last Post: 01-01-2010, 07:33 AM
  2. copy to clipboard not working in firefox
    By madhukar in forum JavaScript
    Replies: 1
    Last Post: 05-29-2009, 01:03 PM
  3. Copy Textarea content to clipboard
    By jnscollier in forum Looking for such a script or service
    Replies: 9
    Last Post: 04-11-2008, 07:53 PM
  4. Copy form input to clipboard
    By sweetboy in forum JavaScript
    Replies: 1
    Last Post: 05-07-2007, 06:55 AM
  5. Replies: 0
    Last Post: 04-02-2007, 08:48 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •