Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Element
  4. copy

Element: copy event

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

Thecopy event of theClipboard API fires when the user initiates a copy action through the browser's user interface.

The event's default action is to copy the selection (if any) to the clipboard.

A handler for this event canmodify the clipboard contents by callingsetData(format, data) on the event'sClipboardEvent.clipboardData property, and cancelling the event's default action usingevent.preventDefault().

However, the handler cannotread the clipboard data.

It's possible to construct and dispatch asyntheticcopy event, but this will not affect the system clipboard.

This eventbubbles up the DOM tree, eventually toDocument andWindow, iscancelable and iscomposed.

Syntax

Use the event name in methods likeaddEventListener(), or set an event handler property.

js
addEventListener("copy", (event) => { })oncopy = (event) => { }

Event type

AClipboardEvent. Inherits fromEvent.

Event ClipboardEvent

Examples

Live example

HTML

html
<div contenteditable="true">Copy text from this box.</div><div contenteditable="true">And paste it into this one.</div>
div.source,div.target {  border: 1px solid gray;  margin: 0.5rem;  padding: 0.5rem;  height: 1rem;  background-color: #e9eef1;}

JavaScript

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

Result

Specifications

Specification
Clipboard API and events
# clipboard-event-copy
HTML
# handler-oncopy

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp