JavaScript Element copy Event

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
DOM event
Instance event

What You’ll Learn

The copy event fires when the user starts a copy action in the browser UI. Learn ClipboardEvent.clipboardData, setData, preventDefault to customize clipboard text, addEventListener vs oncopy, and five try-it labs.

01

Kind

Instance event

02

Type

ClipboardEvent

03

When

User copy action

04

Handler

oncopy

05

Key API

clipboardData.setData

06

Status

Baseline widely available

Introduction

When someone selects text and presses Ctrl+C (or Cmd+C on macOS), or chooses Copy from a menu, the browser fires copy. By default it puts the selection on the system clipboard.

Your handler can change what gets copied: write a custom string with event.clipboardData.setData("text/plain", ...) and call preventDefault() so the default selection copy does not overwrite it.

💡
Beginner tip

A copy handler can write clipboard data but cannot read it. Pair with the paste event when you need to inspect incoming clipboard contents.

Understanding copy

An instance event that answers: “Did the user just start a copy action from the browser UI?”

  • Default — copy the current selection (if any) to the clipboard.
  • ClipboardEvent — use clipboardData.setData(format, data).
  • CancelablepreventDefault() after setting your own data.
  • Bubbles & composed — can reach Document and Window.
  • Baseline Widely available on MDN (since July 2015).

📝 Syntax

Use the event name with addEventListener, or set the handler property:

JavaScript
addEventListener("copy", (event) => { });

oncopy = (event) => { };

Event type

A ClipboardEvent (inherits from Event).

Key property

Property / methodMeaning
clipboardDataDataTransfer object for this clipboard operation
clipboardData.setData(format, data)Write a MIME type such as text/plain or text/html

🔒 What You Can and Cannot Do

  • Can replace copied text with setData + preventDefault().
  • Cannot read clipboard contents inside a copy handler.
  • Cannot change the system clipboard by only dispatching a fake copy event.
  • Siblingscut and paste for the rest of the clipboard story.

⚖️ copy Event vs navigator.clipboard

APIRole
copy eventReact when the user copies via the UI; tweak outgoing data
navigator.clipboard.writeText()Script-initiated copy (needs permissions / secure context)
paste eventReact when the user pastes; can read clipboardData

⚡ Quick Reference

GoalCode / note
Listenel.addEventListener("copy", fn)
Handler propertyel.oncopy = fn
Custom clipboard textevent.clipboardData.setData("text/plain", text) + preventDefault()
Read selectiondocument.getSelection().toString() (not clipboard read)
MDN statusBaseline Widely available (Jul 2015)

🔍 At a Glance

Four facts to remember about copy.

Event type
ClipboardEvent

clipboardData

Write
setData

+ preventDefault

Read?
no

Not in copy handler

Baseline
yes

Widely available

Examples Gallery

Examples follow MDN Element: copy event. Select text, copy with Ctrl+C / Cmd+C, then paste to see the result.

📚 Getting Started

MDN-style clipboard rewrite and the handler property.

Example 1 — Uppercase selection (MDN)

Replace copied text with an uppercase version of the selection.

JavaScript
const source = document.querySelector("div.source");

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

How It Works

You read the live selection, write a new text/plain payload, then cancel the default so the browser does not copy the original casing.

Example 2 — oncopy Property

Same idea using the handler property.

JavaScript
const field = document.getElementById("field");

field.oncopy = (event) => {
  const text = document.getSelection().toString().trim();
  event.clipboardData.setData("text/plain", text);
  event.preventDefault();
};
Try It Yourself

How It Works

Prefer addEventListener when you need multiple listeners or easy removal.

📈 Log, Cite & Soft Block

Observe copies, append a source note, and optionally cancel copy.

Example 3 — Log When Users Copy

Useful for gentle analytics without changing the clipboard.

JavaScript
const article = document.getElementById("article");
const out = document.getElementById("out");

article.addEventListener("copy", () => {
  const len = document.getSelection().toString().length;
  out.textContent = "Copied " + len + " character(s)";
});
Try It Yourself

How It Works

Skipping preventDefault keeps the normal copy behavior while you still observe the action.

Example 4 — Append a Citation

Copy selected text plus a short source line.

JavaScript
const article = document.getElementById("article");

article.addEventListener("copy", (event) => {
  const selected = document.getSelection().toString();
  const payload = selected + "\n\n— Source: CodeToFun tutorial";
  event.clipboardData.setData("text/plain", payload);
  event.preventDefault();
});
Try It Yourself

How It Works

Keep citations short and transparent. Heavy watermarking often frustrates readers.

Example 5 — Soft Block Copy

Cancel the default copy and show a message—demo only, not security.

JavaScript
const secret = document.getElementById("secret");
const out = document.getElementById("out");

secret.addEventListener("copy", (event) => {
  event.preventDefault();
  out.textContent = "Copy disabled in this demo field.";
});
Try It Yourself

How It Works

Blocking copy does not protect content. Prefer clear licensing and better UX over fake DRM.

🚀 Common Use Cases

  • Normalizing copied text (uppercase, trim, strip markdown).
  • Appending a citation or source URL to copied article text.
  • Copying a cleaned plain-text version of rich HTML selections.
  • Logging analytics when users copy content.
  • Softly discouraging copy on a demo field (with clear UX—not DRM).

🔧 How It Works

1

User starts Copy

Shortcut or menu command while focus / selection is in the page.

Input
2

copy event fires

Bubbles; your listener can run on the element, document, or window.

Event
3

Default or custom data

Either the selection is copied, or you setData and cancel the default.

Clipboard
4

Ready to paste

The system clipboard now holds the default or customized payload.

📝 Notes

  • MDN: Baseline Widely available (since July 2015).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Copy handlers write; they do not read clipboard data.
  • Synthetic copy events do not update the system clipboard by themselves.
  • Related learning: contextmenu, cut, addEventListener(), JavaScript hub.

Universal Browser Support

copy is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project. Clipboard MIME types and permissions for scripted clipboard APIs can still vary.

Baseline · Widely available

Element copy

Works across modern desktop and mobile browsers for user-initiated copy actions.

100% Widely available
Google Chrome Supported · Desktop & Android
Full support
Mozilla Firefox Supported · Desktop & Android
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Opera Supported · Modern versions
Full support
Internet Explorer Supported (legacy clipboard model)
Supported
copy Baseline

Bottom line: Listen for copy to customize outgoing clipboard text with setData + preventDefault; use paste when you need to read clipboard data.

Conclusion

copy is the “user is copying” signal. Read the selection, optionally rewrite it with clipboardData.setData, cancel the default, and remember you cannot read the clipboard from this event.

Continue with cut, addEventListener(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Prefer addEventListener("copy", ...)
  • Call setData then preventDefault() for custom text
  • Use document.getSelection() to inspect what the user highlighted
  • Keep attribution short and honest when you append source notes
  • Learn cut and paste alongside copy

❌ Don’t

  • Expect to read clipboard contents in a copy handler
  • Assume a synthetic copy event updates the system clipboard
  • Block all copying as a security feature—users can still screenshot
  • Forget secure-context rules for scripted navigator.clipboard APIs
  • Overwrite oncopy if you need multiple listeners

Key Takeaways

Knowledge Unlocked

Five things to remember about copy

User copy action—write with setData, cancel default, cannot read clipboard here.

5
Core concepts
📄 02

ClipboardEvent

clipboardData

API
✎️ 03

setData

+ preventDefault

Pattern
🚫 04

No read

in copy handler

Limit
🎯 05

Baseline

widely available

Compat

❓ Frequently Asked Questions

It fires when the user starts a copy action through the browser UI (for example Ctrl+C / Cmd+C or Edit → Copy). The default action copies the current selection to the clipboard.
No. MDN marks Element copy as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
A ClipboardEvent. The important property is clipboardData, which you can write to with setData(format, data).
No. MDN states a copy handler cannot read clipboard data. You can write with setData after cancelling the default action, but reading is restricted.
Call event.clipboardData.setData("text/plain", yourText) and event.preventDefault() so the browser does not copy the raw selection instead.
No. You can dispatch a synthetic copy event, but that alone will not put data on the system clipboard.
Did you know?

MDN’s classic demo uppercases the selection before it hits the clipboard. Select “hello”, copy, then paste into another field—you get HELLO.

Next: cut

Learn cut — clipboard write plus remove from the document.

cut →

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