oncopy
Event
Example
Execute JavaScript when text is copied from an <input>
element:
<input type="text" oncopy="myFunction()" value="Try copying this text">
More examples are included at the bottom of this page.
Definition and Usage
The oncopy
event is triggered when the user copies the content of an element.
Tip: The oncopy
event also triggers when the user copies an element, such as an <img>
element.
Tip: The oncopy
event is commonly used with <input>
elements of type text
.
Tip: There are three ways to copy elements and content:
- Press CTRL + C
- Select "Copy" from the Edit menu in your browser
- Right-click and select "Copy" from the context menu.
Browser Support
Event | Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
oncopy | Yes | Yes | Yes | Yes | Yes |
Note: The oncopy
event may not work in Chrome, Firefox, and Opera when trying to copy an image (see more examples).
Syntax
In HTML:
In JavaScript:
In JavaScript, using the addEventListener()
method:
Note: Internet Explorer 8 and earlier versions do not support the addEventListener()
method.
Bubbles: | Yes |
---|---|
Cancelable: | Yes |
--- | --- |
Event type: | ClipboardEvent |
--- | --- |
Supported HTML tags: | All HTML elements |
--- | --- |
More Examples
Example
Execute JavaScript when text content of a <p>
element is copied:
<p oncopy="myFunction()">Try copying this text</p>
Example
Execute JavaScript when an image is copied:
<img src="w3schools.gif" oncopy="myFunction()">
Related Pages
HTML DOM Reference: onpaste Event
HTML DOM Reference: oncut Event