oncopyEvent

事件对象参考手册Event Object

Examples

Execute JavaScript when copying text on an <input> element:

<input type="text" oncopy="myFunction()" value="Try to copy text">

Try it Yourself »

More examples are included at the bottom of this article.


Definition and Usage

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

Tip:The oncopy event also triggers when the user copies an element, for example, copying an <img> element.

Tip:The oncopy event is typically used for <input> elements with 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 the mouse button and select the "Copy" command from the context menu.

Browser Support

Event
oncopy Yes Yes Yes Yes Yes

Note:The oncopy event may not work in Chrome, Firefox, and Opera browsers when trying to copy an image (see more examples).


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:Internet Explorer 8 and earlier IE versions do not supportaddEventListener()method.


Technical Details
Does it support bubbling: Yes
Can it be canceled: Yes
Event type: ClipboardEvent
Supported HTML tags: All HTML elements


Examples

More Examples

Examples

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

<p oncopy="myFunction()">Try copying this text</p>

Try it Yourself »

Examples

Execute JavaScript when copying an image:

<img src="w3schools.gif.html" oncopy="myFunction()">

Try it Yourself »


Related Pages

HTML DOM Reference Manual:onpaste Event

HTML DOM Reference Manual:oncut Event


事件对象参考手册Event Object Other Extensions