The copy
event is fired when the user initiates a copy action through the browser UI (for example, using the CTRL/Cmd+C keyboard shortcut or selecting the "Copy" from the menu) and in response to an allowed {{domxref("Document.execCommand", "document.execCommand('copy')")}} call.
General info
- Specification
- Clipboard
- Interface
- {{domxref("ClipboardEvent")}}
- Bubbles
- Yes
- Cancelable
- Yes
- Target
- {{domxref("Element")}}: the focused element (for {{domxref("HTMLElement.contentEditable", "contentEditable")}} elements - the element containing the start of the selection), or the {{HTMLElement("body")}} element.
- Default Action
- See below.
A handler for this event can modify the provided {{domxref("ClipboardEvent.clipboardData")}} object by calling {{domxref("DataTransfer.setData", "setData(format, data)")}}:
document.addEventListener('copy', function(e){ e.clipboardData.setData('text/plain', 'Hello, world!'); e.clipboardData.setData('text/html', '<b>Hello, world!</b>'); e.preventDefault(); // We want our data, not data from any selection, to be written to the clipboard });
A handler for this event cannot read the clipboard data using {{domxref("DataTransfer.getData", "clipboardData.getData()")}}.
The event's default action depends on the source of the event and the handler's behavior:
- A synthetic copy event does not have a default action; otherwise
- If the event was not cancelled: copies the selection (if any) to the clipboard;
- If a handler cancelled the event and called
setData()
: copies the contents ofclipboardData
to the clipboard; - If a handler cancelled the event without calling
setData()
: no action.
Properties
{{OpenEventProperties()}}{{CloseEventProperties()}}
Browser compatibility
{{ CompatibilityTable() }}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} |
clipboardData |
{{ CompatVersionUnknown() }} | {{ CompatGeckoDesktop(22) }} | {{ CompatNo() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} |
Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | {{ CompatUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} |
clipboardData |
{{ CompatUnknown() }} | {{ CompatGeckoMobile(22) }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} |
Related events
- Copy-related events: {{CopyRelatedEvents()}}