Copy to clipboard

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
tobib
Posts: 7
Joined: Tue Sep 18, 2018

18 Oct 2018

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!

User avatar
AlexZver
Posts: 232
Joined: Mon Aug 27, 2018

19 Oct 2018

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");

tobib
Posts: 7
Joined: Tue Sep 18, 2018

23 Oct 2018

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!

User avatar
AlexZver
Posts: 232
Joined: Mon Aug 27, 2018

23 Oct 2018

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);

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 20 guests