Horje
HTML oncopy Attribute

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.

Elements Event
All HTML elements oncopy

Browser Support

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


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

Input Example.
index.html
Example: HTML
<input type="text" oncopy="myFunction()" value="Try to copy this text">

<p id="demo"></p>

<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "You copied text!"
}
</script>

Output should be:

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

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

P Example.
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

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

<script>
function myFunction() {
  alert("You copied text!");
}
</script>

</body>
</html>

Output should be:

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

How to Execute a JavaScript when copying an image

Img Example.
index.html
Example: HTML
<p>Try to copy the image below (Right click on the image and select "Copy Image").</p>

<img src="https://horje.com/avatar.png" oncopy="myFunction()" alt="The Scream" width="220" height="277">

<p><strong>Note:</strong> This example may not work as expected in some browsers.</p>

<script>
function myFunction() {
  alert("You copied image!");
}
</script>

Output should be:

How to Execute a JavaScript when copying an image




html oncopy attribute

Type:
Develop
Category:
Web Tutorial
Sub Category:
HTML Attribute
Uploaded by:
Admin


Read Article
https://develop.horje.com/learn/1434/reference