# Copy to cliboard

**URL:** <https://forums.creativeclouddeveloper.com/t/copy-to-cliboard/907>\
**Category:** UXP Plugin API\
**Tags:** javascript\
**Created:** [June 10, 2019, 10:21am UTC](https://forums.creativeclouddeveloper.com/t/copy-to-cliboard/907 "2019-06-10T10:21:56Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![Velara](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/velara/32/1278_2.png) [@Velara](https://forums.creativeclouddeveloper.com/u/Velara)\
**Post date:** [July 14, 2019, 7:36am UTC](https://forums.creativeclouddeveloper.com/t/copy-to-cliboard/907/21 "2019-07-14T07:36:22Z")

</div>

> [@nemtsov](#):
>
> document.getElementById(‘container’).innerHTML = `<dialog id="dialog"> <button id="button">copy</button> </dialog>`;

Is it in a dialog window or a plugin panel? A few suggestions:

Try putting the button inside of a form:

```
document.getElementById('container').innerHTML = `
  <dialog id="dialog">
    <form method="dialog"><button id="button">copy</button></form>
  </dialog>
`;

```

Or try using an input:

```
document.getElementById('container').innerHTML = `
  <dialog id="dialog">
    <form method="dialog"><input type="button" id="button">copy</button></form>
  </dialog>
`;

```

It’s possible that if you have any async calls before you get the copyText() method it not be initiated within the click event.

I would also wrap the call in a try catch block and call a known function (not anonymous) and add the event listener before calling showModal() and return a value from showModal().

---

<div class="post-metadata">

**Author:** ![nemtsov](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/nemtsov/32/462_2.png) [@nemtsov](https://forums.creativeclouddeveloper.com/u/nemtsov)\
**Post date:** [July 15, 2019, 3:41pm UTC](https://forums.creativeclouddeveloper.com/t/copy-to-cliboard/907/22 "2019-07-15T15:41:16Z")

</div>

@Velara thanks for replying.

I run the example code from a ui-entry-point with the following wrapper: `export const commands = { main() { ... } };` That’s the only code in the repro plugin. Putting it in a `<form>` has no effect. `<input type="button" />` is unsupported in XD. No async calls are happening. My example code above is the only code in the test plugin (not counting the wrapper).

---

<div class="post-metadata">

**Author:** ![Velara](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/velara/32/1278_2.png) [@Velara](https://forums.creativeclouddeveloper.com/u/Velara)\
**Post date:** [July 15, 2019, 4:35pm UTC](https://forums.creativeclouddeveloper.com/t/copy-to-cliboard/907/23 "2019-07-15T16:35:05Z")

</div>

Change this:

`document.getElementById('dialog').showModal(); document.getElementById('button').addEventListener('click', () => { require('application').editDocument(() => require('clipboard').copyText('hello world') ); });`

To this:

```
function copyToClipboard() {
    var clipboard = require("clipboard");
    clipboard.copyText('hello world');
}

document.getElementById('button').addEventListener('click', copyToClipboard);
var waiting = document.getElementById('dialog').showModal();
```

---

<div class="post-metadata">

**Author:** ![nemtsov](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/nemtsov/32/462_2.png) [@nemtsov](https://forums.creativeclouddeveloper.com/u/nemtsov)\
**Post date:** [July 15, 2019, 9:42pm UTC](https://forums.creativeclouddeveloper.com/t/copy-to-cliboard/907/24 "2019-07-15T21:42:56Z")

</div>

Thanks, tried it. Naming the function has no effect @Velara

---

<div class="post-metadata">

**Author:** ![gdreyv](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/gdreyv/32/358_2.png) [@gdreyv](https://forums.creativeclouddeveloper.com/u/gdreyv)\
**Post date:** [July 15, 2019, 10:12pm UTC](https://forums.creativeclouddeveloper.com/t/copy-to-cliboard/907/25 "2019-07-15T22:12:28Z")

</div>

What is `var waiting =`? Such code doesn’t make any sense. Promise should be awaited by caller. So I suppose it must be `return dialog.showModal();` like it was suggested in the very beginning of the topic.

---

<div class="post-metadata">

**Author:** ![Velara](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/velara/32/1278_2.png) [@Velara](https://forums.creativeclouddeveloper.com/u/Velara)\
**Post date:** [July 16, 2019, 12:23am UTC](https://forums.creativeclouddeveloper.com/t/copy-to-cliboard/907/26 "2019-07-16T00:23:58Z")

</div>

@nemtsov  
I’ve read on here there might be issues with using `innerHTML`. You might want to try the append methods. Can you get any of the basic examples working here? Get those working first and then step by step add your own coding style back into the working example. The UXP environment is not the browser.

Also, it wasn’t clear if you were putting an `await` keyword in front of your `showModal()` call. If you don’t put an `await` in front of your call some events will occur out of the expected order. Assign your event listener before hand.

But if you use `await` then the next line won’t run until the dialog is closed.

@gdreyv  
In the past I have had issues with async and await. Something like the following seemed to fix a lot of issues and in one function I had more than one dialog so I gave them each different names:

```
if (noArtboardSelected) {
  let dialog = await alertDialog.showModal();
  console.log("type of ", typeof dialog); // string
  return dialog;
}
else {
  let dialog = await showMainDialog();
  return dialog;
}
```

[Previous page](https://forums.creativeclouddeveloper.com/t/copy-to-cliboard/907.md?page=1)
