|
| 1 | +--- |
| 2 | +title: "Clipboard: clipboardchange event" |
| 3 | +slug: Web/API/Clipboard/clipboardchange_event |
| 4 | +page-type: web-api-event |
| 5 | +browser-compat: api.Clipboard.clipboardchange_event |
| 6 | +--- |
| 7 | + |
| 8 | +{{APIRef("HTML DOM")}} |
| 9 | + |
| 10 | +The **`clipboardchange`** event of the {{domxref("Clipboard")}} interface is fired when the system clipboard contents are changed in any way, for example via a system copy command, or via an API method such as {{domxref("Clipboard.writeText()")}}. |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property. |
| 15 | + |
| 16 | +```js-nolint |
| 17 | +addEventListener("clipboardchange", (event) => { }) |
| 18 | +
|
| 19 | +onclipboardchange = (event) => { } |
| 20 | +``` |
| 21 | + |
| 22 | +## Event type |
| 23 | + |
| 24 | +A {{domxref("ClipboardChangeEvent")}}. Inherits from {{domxref("Event")}}. |
| 25 | + |
| 26 | +{{InheritanceDiagram("ClipboardChangeEvent")}} |
| 27 | + |
| 28 | +## Examples |
| 29 | + |
| 30 | +### Listening for system copy commands |
| 31 | + |
| 32 | +This example shows how to listen for system copy commands and display the content that was copied to the clipboard. |
| 33 | + |
| 34 | +#### HTML |
| 35 | + |
| 36 | +The HTML consists of three {{htmlelement("p")}} elements — one to display the clipboard contents and two containing sample text to copy. |
| 37 | + |
| 38 | +```html live-sample___basic-usage |
| 39 | +<p id="output">Copied text:</p> |
| 40 | + |
| 41 | +<p> |
| 42 | + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a mattis purus. |
| 43 | + Donec at ipsum libero. Maecenas at dictum turpis. Vivamus eget aliquet augue. |
| 44 | + Aenean tempor dictum posuere. Vestibulum vehicula, nulla ac convallis feugiat, |
| 45 | + tortor velit lobortis est, vitae convallis velit libero vel urna. Suspendisse |
| 46 | + potenti. In bibendum ex et pellentesque gravida. Phasellus magna risus, |
| 47 | + euismod vitae sem in, viverra venenatis lacus. Sed dignissim risus eu congue |
| 48 | + consequat. Vestibulum nec feugiat libero. Maecenas quis sodales lorem, eu |
| 49 | + luctus nisl. Cras vel diam sed lacus finibus elementum sed sed nunc. |
| 50 | +</p> |
| 51 | + |
| 52 | +<p> |
| 53 | + Nam ac metus eget est bibendum pulvinar. Nunc a venenatis lorem. Lorem ipsum |
| 54 | + dolor sit amet, consectetur adipiscing elit. In dignissim, arcu ornare luctus |
| 55 | + pharetra, dui velit faucibus leo, ac posuere ipsum risus vel ligula. Morbi |
| 56 | + varius, felis et ornare efficitur, tortor erat imperdiet lacus, non rhoncus |
| 57 | + lectus sapien sit amet augue. Suspendisse potenti. Sed fringilla mi augue, at |
| 58 | + laoreet felis varius in. Donec venenatis gravida lacus ut rutrum. Donec |
| 59 | + suscipit egestas justo. Proin semper nibh tortor, sit amet elementum metus |
| 60 | + placerat quis. Sed consectetur leo sed lorem varius, sit amet ultrices sem |
| 61 | + tincidunt. Vivamus facilisis at velit eget commodo. |
| 62 | +</p> |
| 63 | +``` |
| 64 | + |
| 65 | +```css hidden live-sample___basic-usage |
| 66 | +body { |
| 67 | + margin: 0 5px; |
| 68 | +} |
| 69 | +#output { |
| 70 | + font-family: Arial, Helvetica, sans-serif; |
| 71 | + padding: 10px; |
| 72 | + border: 2px solid #ccc; |
| 73 | + border-radius: 5px; |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +#### JavaScript |
| 78 | + |
| 79 | +In our script, we first grab a reference to the output `<p>` element. We then define a `clipboardchange` event handler on the browser's {{domxref("Clipboard")}} object. When the event fires, we invoke the {{domxref("Clipboard.readText()")}} method to read the text that was just copied to the clipboard. When the result is returned, we set it as the value of the output paragraph's `textContent`. |
| 80 | + |
| 81 | +```js live-sample___basic-usage |
| 82 | +const outputPara = document.querySelector("#output"); |
| 83 | + |
| 84 | +navigator.clipboard.addEventListener("clipboardchange", (event) => { |
| 85 | + navigator.clipboard |
| 86 | + .readText() |
| 87 | + .then((text) => (outputPara.textContent = `Copied text: ${text}`)); |
| 88 | +}); |
| 89 | +``` |
| 90 | + |
| 91 | +#### Result |
| 92 | + |
| 93 | +The rendered example is as follows: |
| 94 | + |
| 95 | +{{EmbedLiveSample("basic-usage", '100%', "350px", "", "", "", "clipboard-read")}} |
| 96 | + |
| 97 | +Try selecting some text from the example and then copying it to the clipboard using <kbd>Ctrl</kbd> + <kbd>C</kbd> (or <kbd>Cmd</kbd> + <kbd>C</kbd> if you are using a Mac). On your first attempt, you will see a permission prompt asking you to allow permission to read the clipboard contents. After that (or immediately, on subsequent attempts), you should see the text that you copied printed to the output paragraph at the top of the UI. |
| 98 | + |
| 99 | +## Specifications |
| 100 | + |
| 101 | +{{Specifications}} |
| 102 | + |
| 103 | +## Browser compatibility |
| 104 | + |
| 105 | +{{Compat}} |
| 106 | + |
| 107 | +## See also |
| 108 | + |
| 109 | +- {{domxref("ClipboardChangeEvent")}} |
| 110 | +- [Clipboard API](/en-US/docs/Web/API/Clipboard_API) |
0 commit comments