Open In App

HTML oncopy Attribute

Last Updated : 20 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The oncopy Attribute attribute fires when the user copies the content present in an element. The oncopy attribute is used with <img>, <input>, <p> etc elements. It basically triggers on user-initiated copy operations and customizes behavior when content is copied.

Syntax

<element oncopy = "script">

Attribute

This attribute accepts a single value script to be run when oncopy call. 

Note: There are 3 ways to copy the content of an element:

  • Press CTRL + C to copy an element.
  • Select “Copy” from the edit menu.
  • Right-click to display the context menu and select the “Copy” command

Example 1: 

In this example, we will see the implementation of the above event attribute.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>oncopy attribute</title>
    <style>
        body {
            text-align: center;
        }
         
        h1 {
            color: green;
        }
    </style>
</head>
 
<body>
    <h1>GeeksForGeeks</h1>
    <h2>oncopy attribute</h2>
    <input type="text"
           oncopy="Geeks()"
           value="GeeksForGeeks">
    <p id="sudo"></p>
 
    <script>
        function Geeks() {
            document.getElementById("sudo").innerHTML
              = "Copied box content"
        }
    </script>
</body>
 
</html>


Output:

ac

Output

Example 2: 

In this example, we will see the implementation of the above event attribute with another example.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>oncopy attribute</title>
    <style>
        body {
            text-align: center;
        }
         
        h1 {
            color: green;
        }
    </style>
</head>
 
<body>
    <h1>GeeksForGeeks</h1>
    <h2>oncopy attribute</h2>
    <p oncopy="Geeks()">
        GeeksforGeeks: A computer
          science portal for Geeks
    </p>
 
    <p id="sudo"></p>
 
    <script>
        function Geeks() {
            document.getElementById("sudo").
            innerHTML = "Copied Text"
        }
    </script>
</body>
 
</html>


Output:

ax

Output

Supported Browsers

The browser supported by oncopy Event Attribute are listed below:

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 22 and above
  • Opera 12.1 and above
  • Safari 3 and above


Previous Article
Next Article

Similar Reads

HTML DOM oncopy Event
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 + CSelect "Copy" from the Edit menu in your browserRight-click to display the context menu a
2 min read
React onCopy Event
React onCopy Clipboard event is an event handler which detects the copy process in the browser using JavaScript. When the user starts copying data through the shortcut key (CTRL + C) or the copy button present in the menu, this even automatically fires, and the function passed to it will call itself automatically. It is similar to the HTML DOM onco
2 min read
How href attribute is different from src attribute in HTML ?
In HTML5, the href and src attributes play distinct roles in defining paths or URLs, each associated with specific HTML elements. The href attribute, commonly found in an anchor (&lt;a&gt;) elements, points to the destination of hyperlinks, facilitating navigation. The src attribute, used with elements like &lt;img&gt; and &lt;script&gt;, specifies
1 min read
When to use the class attribute and the id attribute ?
The class attribute is used when multiple HTML elements share a common style or behaviour, allowing the application of a shared style to several elements. It promotes code reusability and is suitable for elements with similar characteristics. The id attribute is utilized when a unique identifier is needed for a specific HTML element. This identifie
1 min read
How to use the target attribute and the rel attribute in the &lt;a&gt; Tag ?
The target and rel attributes in the &lt;a&gt; (anchor) tag is used to control the behaviour of hyperlinks, specifying how the linked content should be opened and establishing relationships between the current and linked documents. Here's how to use these attributes: target Attribute:The target attribute determines where the linked content will be
2 min read
HTML | &lt;html&gt; xmlns Attribute
The HTML &lt;html&gt; xmlns Attribute is used to specify the xml namespace for a document. Important Note: This attribute is mainly required in XHTML, not valid in HTML 4.01, and optional in HTML 5. Syntax: &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; Attribute Values: https://www.geeksforgeeks.org/ It defines the namespace to use (for XHTML d
1 min read
HTML | scoped Attribute
The HTML scoped attribute is a boolean attribute that is used to specify that the styles only apply to this element's parent element and that element's child elements (not the entire document).Note : It is a deprecated attribute and it is not supported by HTML5. Supported tag: style Example: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head
1 min read
HTML | &lt;th&gt; valign Attribute
The HTML &lt;th&gt; valign Attribute is used to specify the vertical alignment of text content in a header cell. It is not supported by HTML 5. Syntax: &lt;th valign="top | middle | bottom | baseline"&gt; Attribute Value: top: It sets the table header content to top-align. middle: It sets the table header content to middle-align. bottom: It sets th
1 min read
HTML | &lt;col&gt; align Attribute
The HTML &lt;col&gt; align Attribute is used to set the horizontal alignment of text content inside the col element. It is not supported by HTML 5. Syntax: &lt;col align="left | right | center | justify | char"&gt; Attribute Values: left: It sets the text left-align. right: It sets the text right-align. center: It sets the text center-align. justif
1 min read
HTML | poster Attribute
The HTML poster Attribute is used to display the image while video downloading or when the user clicks the play button. If this image not set then it will take the first frame of video as a poster image.Applicable: It is mainly used in &lt;video&gt; element. Syntax: &lt;video poster="URL"&gt; Attribute Values: It contains a single value URL which s
1 min read