HTML oncopy Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

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.

What You’ll Learn

01

Event handler

Inline JS.

02

copy event

Ctrl+C / Copy.

03

clipboardData

Modify output.

04

vs oncut

Copy vs cut.

05

addEventListener

Preferred way.

06

Attribution

Append source.

Purpose of oncopy Attribute

The 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.

💡
Modify clipboard with clipboardData

In the copy handler, call event.clipboardData.setData("text/plain", value) and event.preventDefault() to replace the default copied text with your own string.

📝 Syntax

Set oncopy on an element with copyable content or assign element.oncopy:

oncopy.html
<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>

Syntax Rules

  • Value is JavaScript code executed when the user copies from the element.
  • Pass event when you need clipboardData or preventDefault().
  • Works on elements with selectable text: div, p, textarea, input, etc.
  • JavaScript: element.oncopy = function(e) { … }.
  • Modern alternative: element.addEventListener("copy", handler).
  • The copy event bubbles — you can delegate on a parent container.

💎 Values

The oncopy attribute accepts a string of JavaScript code:

  • oncopy="handleCopy()" — Call a named function.
  • oncopy="handleCopy(event)" — Pass the event for clipboard access.
  • JavaScript: block.oncopy = (e) => { … } assigns the handler.
oncopy-js.html
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();

});

⚡ Quick Reference

EventWhen it firesHandler
copyUser copies selectiononcopy
cutUser cuts selectiononcut
pasteUser pastesonpaste
clipboardDataIn copy/cut handlersetData / getData
KeyboardCtrl+C / Cmd+CFires copy
Bubbles?copy yesDelegate on parent

Applicable Elements

TargetSupported?Notes
<div>, <p>YesText selections copied from content
<textarea>, <input>YesCopy from form fields
<pre>, <code>YesCommon for code snippets
<img>LimitedCopy image via browser, not text selection
<button>SometimesWhen button label text is selected

oncopy vs oncut vs onpaste

HandlerWhen it firesTypical use
oncopyContent copied to clipboardFeedback, attribution, logging
oncutContent cut (copy + remove)Validate before removing text
onpasteContent pasted from clipboardSanitize pasted input

Examples Gallery

Inline oncopy feedback, addEventListener with clipboardData attribution, and comparing copy with cut events.

👀 Live Preview

Select text in the box and copy it (Ctrl+C or context menu Copy):

Copy this sample paragraph. Select any part of it, then press Ctrl+C.

Copy some text to see the event fire.

Example — Inline oncopy

Show feedback when the user copies content from a div:

inline-oncopy.html
<div id="article" oncopy="handleCopy()">

  Copy this content

</div>



<script>

  function handleCopy() {

    document.getElementById("msg").textContent = "Text copied!";

  }

</script>
Try It Yourself

How It Works

The browser fires copy when the clipboard receives content from the element. Your inline handler runs at that moment.

Dynamic Values with JavaScript

Append a source link to copied text with clipboardData:

dynamic-oncopy.html
document.getElementById("article").addEventListener("copy", function (event) {

  const text = window.getSelection().toString();

  event.clipboardData.setData("text/plain", text + "\n\nSource: example.com");

  event.preventDefault();

});
Try It Yourself

How It Works

Read the selection, write your version to the clipboard, and prevent the browser default so only your data is copied.

Example — copy vs cut

Listen for both clipboard events on the same field:

copy-vs-cut.html
field.addEventListener("copy", () => {

  log("copy — duplicated to clipboard");

});



field.addEventListener("cut", () => {

  log("cut — removed from field");

});
Try It Yourself

How It Works

Both are clipboard events. oncopy handles duplication; oncut handles move-style removal from the source.

♿ Accessibility

  • Do not block copy lightly — Users rely on copying for notes, translation, and assistive tools. Avoid preventDefault() unless you have a clear benefit.
  • Provide feedback — A visible “Copied!” message helps all users, including those who may not notice clipboard changes.
  • Attribution is fine — Appending a source URL is common; keep it short and do not remove the user’s selected text without consent.
  • Avoid alert() on copy — Use inline status text or toast-style messages instead of modal alerts.
  • Keyboard users — Copy via Ctrl+C / Cmd+C must still work when you customize clipboard data in the handler.

🧠 How oncopy Works

1

User selects text

Highlights content to copy.

Selection
2

User copies

Ctrl+C or Copy command.

Action
3

copy event fires

Handler runs on element.

oncopy
=

Clipboard updated

Default or custom data.

Browser Support

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.

DOM Events · Fully supported

Universal oncopy support

All major browsers fire the underlying event and honor the oncopy handler attribute.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 9+ supported
Full support
Opera Fully supported
Full support
oncopy attribute 99% supported

Bottom line: Universal support for the copy event. Test clipboard customization with keyboard copy and context menu Copy.

💡 Best Practices

✅ Do

  • Use addEventListener on the element — Clearer than inline oncopy.
  • Use clipboardData thoughtfully — Append attribution rather than replacing user content unexpectedly.
  • Show copy feedback — A brief “Copied!” toast improves UX without blocking the clipboard.
  • Pair with oncut and onpaste — Handle the full clipboard lifecycle when needed.
  • Respect security — Do not use copy handlers to exfiltrate data the user did not intend to share.

❌ Don’t

  • Do not disable copy site-wide — Blocking copy frustrates users and is easy to bypass.

Conclusion

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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about oncopy

Bookmark these before wiring your next event handler.

5
Core concepts
🌐 02

Text content

div, p, input.

Scope
🔄 03

clipboardData

Customize output.

Pattern
📝 04

addEventListener

Preferred.

Modern
05

vs oncut

Copy vs cut.

Note

❓ Frequently Asked Questions

It runs JavaScript when the copy event fires — when the user copies content from an element.
Elements with copyable text: div, p, textarea, input, and more.
Yes. Use event.clipboardData.setData() and event.preventDefault() in the copy handler.
oncopy duplicates content. oncut copies and removes it from the source.
Prefer element.addEventListener("copy", …). Inline oncopy works for simple demos.
Yes. One listener on a parent can handle copy events from child elements.

Handle copy events with JavaScript

Practice inline oncopy, clipboardData attribution, and copy vs cut in the Try It editor.

Try oncopy demo →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful