Copy content of rich editor using Javascript

I have a rich text editor on my page, specifically this one:

I need to copy the content of this editor into a different rich text editor on a different website.

I can do this manually, by selecting the content of the editor and copying it. When I paste into the target editor, I get the correct outcome: the text is formatted just like in the original editor, including links etc.

The problem is, I need to do the same using a "copy to clipboard" button. I have tried using this on the editor element directly:;

…but nothing ends up in the clipboard. I have tried putting the content into a regular input element and then doing the same thing above from that, but then when I paste in the target editor I get the raw HTML instead of the formatted text.

In short, if the content editor is this…

One line

Two lines

…I need it to paste into the target (I reiterate, on a different website) like this…

One line

Two lines

…and not like this:

<p>One line</p><p>Two lines</p>

85 thoughts on “Copy content of rich editor using Javascript”