Jump to content

Copying Text to Clipboard

Recommended Posts

Any help on how I can click a label/ text to copy to clipboard?

Here is my code, but its not working.

procedure TForm1.W3Label1Click(Sender: TObject);

     W3Label1.Caption:= '<id="ACC">1234';

   var document.getElementById('ACC');
    ShowMessage("Copied the text: " + copyText.value);


Share this post

Link to post
Share on other sites

This works and copies text to clipboard

     browserapi.document.designMode := 'on';
         navigator.clipboard.writeText("copy this text");

I'm still unable to introduce a string variable from label or input box to replace "copy this text" - Any idea how I can do that?

Share this post

Link to post
Share on other sites

Browsers pick up selected text in <textarea> and <p> type of html elements, not necessarily in each and every other type. Also they will not mess with existing clipboard data unless initiated by a user action (like clicking a button).

Bit of a roundabout way of doing this, but this works

(form with W3Label1 and W3Button1)

procedure TForm1.W3Button1Click(Sender: TObject);
var xCopy : Procedure(S:String);
@xCopy = function copyToClipboard(text) {
    if (window.clipboardData && window.clipboardData.setData) {
        // IE specific code path to prevent textarea being shown while dialog is visible.
        return clipboardData.setData("Text", text);

    } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
        var textarea = document.createElement("textarea");
        textarea.textContent = text;
        textarea.style.position = "fixed";  // Prevent scrolling to bottom of page in MS Edge.
        try {
            return document.execCommand("copy");  // Security exception may be thrown by some browsers.
        } catch (ex) {
            console.warn("Copy to clipboard failed.", ex);
            return false;
        } finally {



Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now