👀 Live Preview
Select text in the box and copy it (Ctrl+C or context menu Copy):
Copy some text to see the event fire.

The oncopy attribute is an inline event handler that runs JavaScript when the copy event fires. That happens when the user copies content — with Ctrl+C (Cmd+C on Mac), Edit → Copy, or the context menu Copy action. It is useful for showing feedback, logging analytics, or modifying clipboard data with event.clipboardData. The copy event bubbles, so you can listen on a parent container. For production sites, prefer addEventListener("copy", …) over inline handlers.
Inline JS.
Ctrl+C / Copy.
Modify output.
Copy vs cut.
Preferred way.
Append source.
oncopy AttributeThe primary purpose of oncopy is to react when the user copies text or content from an element. Common uses include showing a “Copied!” message, tracking analytics, appending a source link to clipboard text, or validating that copied content is allowed to leave the page. Because the handler runs during the copy operation, you can influence what lands on the clipboard via event.clipboardData.
Do not confuse oncopy with oncontextmenu. Right-click opens the menu; copy fires when the user actually copies. Pair with oncut and onpaste for full clipboard event coverage. Avoid blocking copy without a good reason — users rely on copying for accessibility and productivity.
In the copy handler, call event.clipboardData.setData("text/plain", value) and event.preventDefault() to replace the default copied text with your own string.
Set oncopy on an element with copyable content or assign element.oncopy:
<script>
function handleCopy() {
document.getElementById("status").textContent = "Content copied!";
}
</script>
<div id="article" oncopy="handleCopy()">
Select and copy this paragraph.
</div>
<p id="status"></p>event when you need clipboardData or preventDefault().div, p, textarea, input, etc.element.oncopy = function(e) { … }.element.addEventListener("copy", handler).copy event bubbles — you can delegate on a parent container.The oncopy attribute accepts a string of JavaScript code:
oncopy="handleCopy()" — Call a named function.oncopy="handleCopy(event)" — Pass the event for clipboard access.block.oncopy = (e) => { … } assigns the handler.const article = document.getElementById("article");
article.addEventListener("copy", (event) => {
const selection = window.getSelection().toString();
event.clipboardData.setData("text/plain", selection + "\n\nSource: example.com");
event.preventDefault();
});| Event | When it fires | Handler |
|---|---|---|
copy | User copies selection | oncopy |
cut | User cuts selection | oncut |
paste | User pastes | onpaste |
clipboardData | In copy/cut handler | setData / getData |
| Keyboard | Ctrl+C / Cmd+C | Fires copy |
| Bubbles? | copy yes | Delegate on parent |
| Target | Supported? | Notes |
|---|---|---|
<div>, <p> | Yes | Text selections copied from content |
<textarea>, <input> | Yes | Copy from form fields |
<pre>, <code> | Yes | Common for code snippets |
<img> | Limited | Copy image via browser, not text selection |
<button> | Sometimes | When button label text is selected |
oncopy vs oncut vs onpaste| Handler | When it fires | Typical use |
|---|---|---|
oncopy | Content copied to clipboard | Feedback, attribution, logging |
oncut | Content cut (copy + remove) | Validate before removing text |
onpaste | Content pasted from clipboard | Sanitize pasted input |
Inline oncopy feedback, addEventListener with clipboardData attribution, and comparing copy with cut events.
Select text in the box and copy it (Ctrl+C or context menu Copy):
Copy some text to see the event fire.
Show feedback when the user copies content from a div:
<div id="article" oncopy="handleCopy()">
Copy this content
</div>
<script>
function handleCopy() {
document.getElementById("msg").textContent = "Text copied!";
}
</script>The browser fires copy when the clipboard receives content from the element. Your inline handler runs at that moment.
Append a source link to copied text with clipboardData:
document.getElementById("article").addEventListener("copy", function (event) {
const text = window.getSelection().toString();
event.clipboardData.setData("text/plain", text + "\n\nSource: example.com");
event.preventDefault();
});Read the selection, write your version to the clipboard, and prevent the browser default so only your data is copied.
Listen for both clipboard events on the same field:
field.addEventListener("copy", () => {
log("copy — duplicated to clipboard");
});
field.addEventListener("cut", () => {
log("cut — removed from field");
});Both are clipboard events. oncopy handles duplication; oncut handles move-style removal from the source.
preventDefault() unless you have a clear benefit.Highlights content to copy.
Ctrl+C or Copy command.
Handler runs on element.
Default or custom data.
The copy event and oncopy handler are supported in all modern browsers — Chrome, Firefox, Safari, and Edge. clipboardData customization works in current browsers during the copy event.
oncopy supportAll major browsers fire the underlying event and honor the oncopy handler attribute.
Bottom line: Universal support for the copy event. Test clipboard customization with keyboard copy and context menu Copy.
oncopy.The oncopy attribute runs JavaScript when the user copies content from an element and the copy event fires. Use it for feedback, analytics, or customizing clipboard text with event.clipboardData.
Prefer addEventListener("copy", …), respect user expectations, and pair with oncut and onpaste when building clipboard-aware features.
oncopyBookmark these before wiring your next event handler.
copy fires.
Eventdiv, p, input.
ScopeCustomize output.
PatternPreferred.
ModernCopy vs cut.
Notecopy event fires — when the user copies content from an element.div, p, textarea, input, and more.event.clipboardData.setData() and event.preventDefault() in the copy handler.oncopy duplicates content. oncut copies and removes it from the source.element.addEventListener("copy", …). Inline oncopy works for simple demos.Practice inline oncopy, clipboardData attribution, and copy vs cut in the Try It editor.
5 people found this page helpful