THE WORLD'S LARGEST WEB DEVELOPER SITE

oncopy Event

❮ Event Object

Example

Execute a JavaScript when copying some text of an <input> element:

<input type="text" oncopy="myFunction()" value="Try to copy this text">
Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

The oncopy event occurs when the user copies the content of an element.

Tip: The oncopy event also occurs when the user copies an element, for example, an image, created with the <img> element.

Tip: The oncopy event is mostly used on <input> elements with type="text".

Tip: There are three ways to copy an element/the content of an element:

  • Press CTRL + C
  • Select "Copy" from the Edit menu in your browser
  • Right click to display the context menu and select the "Copy" command.

Browser Support

Event
oncopy Yes Yes Yes Yes Yes

Note: The oncopy event may not work as expected in some browsers when trying to copy an image (See "More Examples" below).



Syntax

In HTML:

<element oncopy="myScript">
Try it Yourself »

In JavaScript:

object.oncopy = function(){myScript};
Try it Yourself »

In JavaScript, using the addEventListener() method:

object.addEventListener("copy", myScript);
Try it Yourself »

Note: The addEventListener() method is not supported in Internet Explorer 8 and earlier versions.


Technical Details

Bubbles: Yes
Cancelable: Yes
Event type: ClipboardEvent
Supported HTML tags: All HTML elements

More Examples

Example

Execute a JavaScript when copying some text of a <p> element:

<p oncopy="myFunction()">Try to copy this text</p>
Try it Yourself »

Example

Execute a JavaScript when copying an image:

<img src="w3schools.gif" oncopy="myFunction()">
Try it Yourself »

Related Pages

HTML DOM reference: onpaste event

HTML DOM reference: oncut event


❮ Event Object