click on copy button using javascript

142 Views Asked by At

I am trying to click on copy button on an editor using Javascript for automation purposes but the text not stored in the clipboard

tried the below codes

  const button = document.getElementById("copy-1");
button.click();

and

   const button = document.getElementById("copy-1");
   const event = new Event("click");
   button.dispatchEvent(event);

the HTML code

<button id="copy-1" type="button" tabindex="-1" role="button" class="fr-command fr-btn" data-cmd="copy" data-title="Copy" style="outline: 1px solid blue;">
    <froalacopy_button class="hoverable"><i class="fas fa-copy"></i></froalacopy_button><span class="fr-sr-only">Copy</span>
</button>

also, I tried to copy it directly but the copied text was not formatted

enter image description here

1

There are 1 best solutions below

0
Hetal N On

According to Froala Editor you do not need to handle the custom button click event manually. You can add a button using its methods like DefineIcon and RegisterCommand.

In RegisterCommand you will get the callback method which will handle the onclick event of that button.

FroalaEditor.DefineIcon('insert', {NAME: 'plus', SVG_KEY: 'add'});
FroalaEditor.RegisterCommand('insert', {
  title: 'Copy the selected text',
  focus: true,
  undo: true,
  refreshAfterCallback: true,
  callback: function () {
    document.execCommand("copy");
  }
});

You can check more from here