Open In App

HTML DOM oncopy Event

Last Updated : 22 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The HTML DOM oncopy event occurs when the content of an element is copied by the user. It is also applicable to the image, created with the element. It is mostly used with type=”text”.

Note: There are three ways to copy 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.

Supported Tags 

  • It supports all HTML elements

Syntax:

In HTML: 

<element oncopy="myScript">

In JavaScript:  

object.oncopy = function(){myScript};

In JavaScript, using the addEventListener() method: 

object.addEventListener("copy", myScript);

Example: Using HTML 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>
          HTML DOM oncopy event
      </title>
</head>
 
<body>
    <center>
        <h1 style="color:green">GeeksforGeeks</h1>
        <h2>HTML DOM oncopy event</h2>
        <input type="text"
               oncopy="myFunction()"
               value="Copy the text">
        <p id="demo"></p>
 
        <script>
            function myFunction() {
                document.getElementById("demo")
                  .innerHTML = "Done"
            }
        </script>
    </center>
</body>
 
</html>


Output: 

 

Example: Using JavaScript 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>
          HTML DOM oncopy event
      </title>
</head>
 
<body>
    <h1 style="color:green">
          GeeksforGeeks
      </h1>
    <h2>HTML DOM oncopy event</h2>
    <input type="text"
           id="myInput"
           value="Copy the text">
   
    <script>
        document.getElementById("myInput")
          .oncopy = function () { GFGfun() };
        function GFGfun() {
            alert("Done");
        }
    </script>
 
</body>
 
</html>


Output: 

 

Example: In JavaScript, using the addEventListener() method: 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>
          HTML DOM oncopy event
      </title>
</head>
 
<body>
    <center>
        <h1 style="color:green">
            GeeksforGeeks
        </h1>
        <h2>HTML DOM oncopy event</h2>
        <input type="text"
               id="myInput"
               value="Copy the text">
 
        <script>
            document.getElementById(
                "myInput")
                .addEventListener("copy", GFGfun);
 
            function GFGfun() {
                alert("Done");
            }
        </script>
    </center>
</body>
 
</html>


Output: 

 

Supported Browsers: The browsers supported by oncopy Event are listed below: 

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Apple Safari
  • Opera


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads