View Full Version : Copy text from form to clipboard.
rechmbrs
02-21-2020, 07:17 PM
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:confused:
keyboard
02-26-2020, 11:41 PM
Add a javascript function
function copyToClip(form)
{
form.display.select();
form.display.setSelectionRange(0, 99999);
document.execCommand("copy");
}
Add a new button
<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.
rechmbrs
02-27-2020, 05:52 AM
Thanks for the response.
For the function, I found this and it works.
JS FUNCTION:
function copy2Clipboard(input)
{
try
{
navigator.clipboard.writeText(input.value); // Copy text
}
catch (err) // Catch errors
{
alert("Failed to copy: " + err);
}
}
HTML:
<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
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.