HTML oncopy Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The oncopy attribute is an event-handler attribute. It runs JavaScript when the copy event fires — the user starts a copy through the browser UI (for example Ctrl+C / Cmd+C, or a Copy menu item).

By default, the selection is copied to the clipboard. Your handler can replace that data with event.clipboardData.setData(…) and preventDefault(). It cannot read the clipboard. Related events: oncut and onpaste.

Baseline · Widely available.

Not deprecated and not experimental. The copy event is stable across modern browsers since about July 2015. It bubbles, is cancelable, and is composed.

Prefer addEventListener in apps.

Inline oncopy is fine for learning. Production code usually uses el.addEventListener("copy", handler). Blocking copy is a weak “protection” — users can still capture content other ways.

HTML Syntax

syntax.html
<element oncopy=" script">

script is JavaScript that runs when copy fires. Equivalent JS: el.oncopy = handler or el.addEventListener("copy", handler).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when the user initiates copyTry it
oncopy="fn()"Call a named function from the attributeTry it
clipboardData.setDataWrite custom text/HTML to the clipboard (with preventDefault)Try it
preventDefault() onlyCancel the default copy (selection is not placed on the clipboard)Try it
addEventListener("copy")Preferred modern listener on the element or documentTry it

Live Preview

Select text in the box, then copy (Ctrl+C / Cmd+C). Toggle options to transform or block the copy.

On copy

Paste somewhere else to verify uppercase or blocked modes.

Selectable text

Select this sample text, then copy it.

Waiting for copy…

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Inline oncopy on a Textarea

Show a status message when the user copies from the field.

HTML
<label for="notes">Notes:</label>
<textarea id="notes" rows="3" cols="40" oncopy="onCopyNote()">
Select some text and copy it.
</textarea>
<p id="status">Waiting for copy…</p>

<script>
  function onCopyNote() {
    document.getElementById("status").textContent = "copy event fired.";
  }
</script>
Try It Yourself

How It Works

  1. User selects text in the textarea.
  2. User copies via keyboard or UI — copy fires on the element.
  3. oncopy runs onCopyNote() and updates status.
  4. Default still copies the selection unless you call preventDefault().

Example 2 — Put Uppercase Text on the Clipboard

Replace the copied selection with a transformed string.

HTML
<div id="source" contenteditable="true">
  Copy text from this box.
</div>
<p id="hint">Paste elsewhere to see UPPERCASE.</p>

<script>
  document.getElementById("source").addEventListener("copy", function (event) {
    var selection = document.getSelection();
    event.clipboardData.setData(
      "text/plain",
      selection.toString().toUpperCase()
    );
    event.preventDefault();
  });
</script>
Try It Yourself

How It Works

  1. Read the current selection with document.getSelection().
  2. Write new data with clipboardData.setData("text/plain", …).
  3. Call preventDefault() so the browser does not also write the raw selection.
  4. Paste elsewhere to confirm the transformed text.

Example 3 — Cancel the Default Copy

Stop the selection from being placed on the clipboard.

HTML
<p id="secret" oncopy="blockCopy(event)">
  Try to copy this sentence.
</p>
<p id="log"></p>

<script>
  function blockCopy(event) {
    event.preventDefault();
    document.getElementById("log").textContent =
      "Copy blocked (default action canceled).";
  }
</script>
Try It Yourself

How It Works

  1. User tries to copy from the paragraph.
  2. preventDefault() cancels the default clipboard write.
  3. Status updates so the student sees the event still ran.
  4. Not real DRM — users can still retype, screenshot, or view source.

Edge Cases & Pitfalls

Most mistakes are trying to read the clipboard on copy, or treating preventDefault as content protection.

read

Cannot read clipboard in copy

The handler may write with setData, but it cannot read clipboard contents. Reading belongs to other APIs / paste flows with permissions.

setData

Remember preventDefault with setData

If you set custom data but forget to cancel the default, the browser may still write the raw selection.

synthetic

Synthetic copy events

Dispatching a fake copy event does not change the system clipboard. Only a real user-initiated copy (or the Async Clipboard API) does.

vs cut

Copy ≠ cut

oncut also removes the selection from the document. Use the matching event for your UX.

a11y

Do not block copy without a reason

Blocking copy frustrates users and assistive workflows. Prefer clear licensing text over disabling clipboard.

Browser Support

oncopy is an HTML event-handler attribute for the Clipboard API in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

oncopy

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support the copy event on selectable content.

97% Global support (approx.)
Google Chrome 1+
Full support
Mozilla Firefox 22+
Full support
Apple Safari 3+
Full support
Microsoft Edge 12+
Full support
Opera 15+
Full support
Samsung Internet Modern · Full
Full support
Internet Explorer 9+
Supported
oncopy Widely supported

Bottom line: Safe to use for reacting to user copy. Prefer addEventListener in production, and use setData + preventDefault only when you intentionally change clipboard content.

Key Takeaways

  • Role: run script when the user initiates a copy.
  • Write: clipboardData.setData + preventDefault to customize.
  • No read: the copy handler cannot read the clipboard.
  • Family: pair with oncut / onpaste as needed.

One line: oncopy reacts to a user copy — you can change what is written, but you cannot read the clipboard here.

Frequently Asked Questions

It is an event-handler attribute. When the user starts a copy action through the browser UI (for example Ctrl+C / Cmd+C or a Copy menu item), the copy event fires and the JavaScript in oncopy runs.
Yes. In the handler, call event.clipboardData.setData("text/plain", yourText) and event.preventDefault() so your data replaces the default selection copy.
No. A copy event handler cannot read clipboard contents. It can only write via clipboardData.setData after canceling the default action.
oncopy runs when copying; oncut when cutting (copy + remove); onpaste when pasting into a target. All are Clipboard API events on the element (and they bubble).
Both work. For learning, oncopy in HTML is fine. In production code, prefer element.addEventListener("copy", handler) so you can attach multiple listeners and keep script out of markup.
No. The copy event and oncopy handler are standard and Baseline widely available since about July 2015.

Next: oncuechange

React when a text track cue changes on media elements.

oncuechange tutorial →

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.

8 people found this page helpful