HTML oncopy Attribute (2024)

Definition and Usage

The oncopy attribute fires when the user copies the content of an element.

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

Tip: The oncopy attribute 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

Applies to

The oncopy attribute is part of the Event Attributes, and can be used on any HTML elements.

ElementsEvent
All HTML elementsoncopy

Examples

Input 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 »

P Example

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

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

Try it Yourself »

Img Example

Execute a JavaScript when copying an image:

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

Try it Yourself »

Browser Support

Event Attribute
oncopy Yes Yes Yes Yes Yes

Note: The oncopy attribute may not work as expected in some browsers when trying to copy an image (See example above).

W3schools Pathfinder

Track your progress - it's free!

HTML oncopy Attribute (2024)
Top Articles
Latest Posts
Article information

Author: Patricia Veum II

Last Updated:

Views: 6313

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Patricia Veum II

Birthday: 1994-12-16

Address: 2064 Little Summit, Goldieton, MS 97651-0862

Phone: +6873952696715

Job: Principal Officer

Hobby: Rafting, Cabaret, Candle making, Jigsaw puzzles, Inline skating, Magic, Graffiti

Introduction: My name is Patricia Veum II, I am a vast, combative, smiling, famous, inexpensive, zealous, sparkling person who loves writing and wants to share my knowledge and understanding with you.