copy selection of text to clipboard

How do I cut or copy just a selection of text (not the entire contents)
from a (random) textarea to the clipboard with a method?

Regards,
Ron

You can use:

application.setClipboardContent(elements.myElement.getSelectedText());

to get the selected text as a string and copy it to the clipboard.
After that you can replace the selected text with an empty string to simulate cutting:

elements.myElement.replaceSelectedText("");

Thanks Joas.
Your code works fine with a dedicated element but what if
I have e.g. two textareas and I want to use the same copybutton,
for these two areas. Is there a way to ‘get’ the elementname?

Regards,
Ron

Ron:
Is there a way to ‘get’ the elementname?

There is:

var _elt = application.getMethodTriggerElementName();
application.setClipboardContent(elements[_elt].getSelectedText());

Hi Joas,

I get this error trying your code:

Conversion Error. The undefined value has no properties calling
getSelectedText.

What can be wrong?

Regards,
Ron

Hi Ron,

Make sure your form fields have the name property filled.

Hope this helps.

Yes the name properties were set Robert (that was yesterday’s lesson!).
The problem imho is that the focus of the field is lost while clicking the
‘copybutton’.
There is absolutely no problem when calling the method from the menu.
Now I know this was also the case with the first code of Joas…

Regards,
Ron

Ah yes,

I guess you need to use a onFocusGained method on the fields that stores the fieldname in a global. Your copy2clipboard method can use this when you click on the button.

Hope this gets you further.

Your suggestion works like a charm Robert, thx.
Bob Cusick uses to say: ‘remember Servoy is not FileMaker’ (!).

For newbies like me this is what I did:

  1. make global textfield: globals.gElName

  2. make method: ElementNameFocus
    attach method onFocusGained to all desired textfields
    code: globals.gElName = application.getMethodTriggerElementName()

  3. make method: CopySelection
    attach method onAction to a button
    code: application.setClipboardContent(elements[gElName].getSelectedText());

Regards,
Ron