Copy Text on Click

Was helping someone with this and wanted to post here in case its helpful.

If you want to make an element copyable (real word?) by simply clicking on it. Here’s some brief instuctions.

First add the JS Notify library so we can show a notifcation when something is copied.

Header of the App:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/notify/0.4.2/styles/metro/notify-metro.min.css" integrity="sha512-PlmS4kms+fh6ewjUlXryYw0t4gfyUBrab9UB0vqOojV26QKvUT9FqBJTRReoIZ7fO8p0W/U9WFSI4MAdI1Zm+A==" crossorigin="anonymous" />

Footer:
<script src="https://cdnjs.cloudflare.com/ajax/libs/notify/0.4.2/notify.min.js" integrity="sha512-efUTj3HdSPwWJ9gjfGR71X9cvsrthIA78/Fvd/IN+fttQVy7XWkOAXb295j8B3cmm/kFKVxjiNYzKw9IQJHIuQ==" crossorigin="anonymous"></script>

Javascript:

var flag = false;
function checkFlag() {
    if(flag === false) {
        if ($('.copyabletext').html() !== undefined || i > 30) {
            flag = true;
        }
       window.setTimeout(checkFlag, 100);
    } else {
        var elements = document.getElementsByClassName("copyabletext");

        var myFunction = function() {
            console.log("Clicked");
            navigator.clipboard.writeText(this.innerText)
            $.notify("Copied to clipboard", "success");
        };

        for (var i = 0; i < elements.length; i++) {
        elements[i].addEventListener('click', myFunction, false);
            
    }
    }
}

checkFlag();

CSS:

.copyabletext {
    cursor: copy;
}

Next anything that has the CSS Class of copyabletext will be copyable.

For example, if there is a column in your table that you’d like the values to be easliy copied, simply add the class in the column settings:

7 Likes

That’s really cool, thanks for sharing!

1 Like

Hi
I want to implement this on my application . So where should I put those header and footer part code(link) in my app.

@bas123, Here’s a screenshot of where to find that:

1 Like

This code goes in the custom Header section of your appss settings page

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/notify/0.4.2/styles/metro/notify-metro.min.css" integrity="sha512-PlmS4kms+fh6ewjUlXryYw0t4gfyUBrab9UB0vqOojV26QKvUT9FqBJTRReoIZ7fO8p0W/U9WFSI4MAdI1Zm+A==" crossorigin="anonymous" />

This code goes in the custom Footer section of your apps settings page

<script src="https://cdnjs.cloudflare.com/ajax/libs/notify/0.4.2/notify.min.js" integrity="sha512-efUTj3HdSPwWJ9gjfGR71X9cvsrthIA78/Fvd/IN+fttQVy7XWkOAXb295j8B3cmm/kFKVxjiNYzKw9IQJHIuQ==" crossorigin="anonymous"></script>


You can put the JavaScript and CSS code in a page layout so that it applies to every page within the layout.

Just remember to add copyabletext as a custom class to any field you want to be copyable - see Moe’s original post for that.

I was playing around with this and noticed that if you assign an entire component the copyabletext CSS class, literally the entire gets copied when you click anywhere within it. The text below was copied with a single click.

Customers (sample)
3 Records
CUSTOMER PROFILE EMAIL STATUS CONTACT DATE SCHEDULE MEETING
David Ross
davidross@example.com Won 08/15/2019
Schedule Meeting

Tina Write
tinawrite@example.com Won 09/25/2019
Schedule Meeting

Larry Johnson
ljohnson@example.com Won 07/01/2019
Schedule Meeting

1 Like

Thank you Tim, for the detailed information. I did it in the same way. And it is working to some extent.
But still, I have the following issue.
I am trying to do the same as in the video of Moe. I have to assign a CSS class to the first column of the table as copyabletext. Now I find out that it is working for only the first row i.e title of the column as following screen shoot I have attached. I want to copy the column value as in Moes video, but in my case, it is only working for the title of the column.

When I click just below the title it is not working .

I have added the class name of column in following way:

Hi Tim , thank you for your help . Actually now it is working as I wanted . Thanks again for explaining in details.

1 Like

How can I implement the code in a html page with a button that will copy a field from the html page?

Regards
Eric

Here’s a video for how you can create a copy button on a detail page through an HTML component. The solution is a slightly modified version of the one we briefly discussed here.

Below you can find the code snippets I reference throughout the video. Be sure to take note of the values I mention in the video that should be changed in the HTML source code and JavaScript.

HTML Component source code

<p>{Long Text}</p>
<p><button id="copy">Copy text</button></p>
<p><input id="myInput" type="text" value="{!!Long Text!!}" /></p>

JavaScript

TB.render('component_4', function(data) {
	$("#copy").click(function() {
	    var copyText = document.getElementById("myInput");
	    copyText.select();
        copyText.setSelectionRange(0, 99999);
        navigator.clipboard.writeText(copyText.value);
        alert("Copied the text: " + copyText.value);
	});
});

CSS

#myInput{
    display:none;
}
3 Likes

Hi @bas123 @tim.young I have the same problem you described. (The text is copied if I click on the colum title, but not on the cells)
Can you please explain what was the solution?

Tanks!!!

I Solved it!
If anyone experience the same problem, the solution is to add more time to the timeout in this line of the javascript:

window.setTimeout(checkFlag, 100);

I changed the 100 for 1000 and it worked (seems that my table takes longer to load).

I found this thread really useful thank you everyone who contributed. I was actually trying to copy and paste Richtext data which I achieved very simply my modifying the Javascript ever so slightly so it grabbed the HTML text rather than just the text by modifying this line… :+1:

navigator.clipboard.writeText(this.innerHTML)

Javascript:

var flag = false;
function checkFlag() {
    if(flag === false) {
        if ($('.copyabletext').html() !== undefined || i > 30) {
            flag = true;
        }
       window.setTimeout(checkFlag, 100);
    } else {
        var elements = document.getElementsByClassName("copyabletext");

        var myFunction = function() {
            console.log("Clicked");
            navigator.clipboard.writeText(this.innerHTML)
            $.notify("Copied to clipboard", "success");
        };

        for (var i = 0; i < elements.length; i++) {
        elements[i].addEventListener('click', myFunction, false);
            
    }
    }
}

checkFlag();