Page 1 of 1

Copy to clipboard

Posted: 18 Oct 2018
by tobib
Hi all,

I want to copy the text of a text field to the clipboard.

Apparently, the usual JavaScript way does not work.

In a button I have implemented the following code. Any idea what's wrong about it?

var copytext = fd.field('Status').control().value()
copytext.select();
document.execCommand("copy");

I have also tried to use the command fd.execCommand("copy") instead of document.execCommand("copy"), but it didn't work.

Thank you very much in advance for help!

Re: Copy to clipboard

Posted: 19 Oct 2018
by AlexZver
Hi!

You should change a code a little bit:

1) For a single-line text field:

Code: Select all

fd.field('Status').control()._el().find('input').select();
document.execCommand("copy");
2) For a multi-line text field:

Code: Select all

fd.field('Status').control()._el().find('textarea').select();
document.execCommand("copy");

Re: Copy to clipboard

Posted: 23 Oct 2018
by tobib
Hi,

thanks for your help. It works, but only when the form is used in edit mode.

I guess this is because the field cannot be selected in display mode.

Is there a way to copy the text of a variable to the clipboard?

If we take the above-mentioned example. Why can't I select "copytext"? Or how can I do that?

Thank you very much in advance!

Re: Copy to clipboard

Posted: 23 Oct 2018
by AlexZver
Hi!

'Copytext' will be just a value in the example above, not a DOM element, so select() method will be useless for it.

Please try to use this code for the Display Form to select an element (you can test it in the browser console):

Code: Select all

rng = document.createRange();
rng.selectNode(fd.field('FieldInternalName').control()._el()[0]);
sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(rng);