JavaScript Element paste Event

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

What You’ll Learn

The paste event fires when the user starts a paste action in the browser UI. Learn clipboardData.getData, how to cancel and transform pasted text, onpaste, editable contexts, and five try-it labs.

01

Kind

Instance event

02

Type

ClipboardEvent

03

When

User paste action

04

Handler

onpaste

05

Key APIs

getData · preventDefault

06

Status

Baseline widely available

Introduction

paste answers: “Did the user just paste through the browser UI?” If the cursor is in an editable context (an <input>, <textarea>, or an element with contenteditable), the default action inserts clipboard contents at the caret.

Unlike copy / cut, a paste handler can read clipboard data via event.clipboardData.getData(...). That makes paste the place to sanitize, transform, or block inbound clipboard content.

💡
Beginner tip

To insert different data than the clipboard holds, call event.preventDefault(), then insert your own text with the Selection API (MDN’s uppercase demo pattern).

Understanding paste

An instance event that answers: “Did the user initiate paste?”

  • Fires when paste starts through the browser UI (keyboard, menu, etc.).
  • ClipboardEvent — read with clipboardData.getData(format).
  • Default — insert clipboard contents into an editable context.
  • CancelablepreventDefault() stops the default insert so you can paste custom data.
  • Bubbles — up through the DOM to Document and Window; also composed.
  • Baseline Widely available on MDN (since July 2015).

📝 Syntax

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

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

onpaste = (event) => { };

Event type

A ClipboardEvent (inherits from Event).

Handy properties

Property / methodMeaning
clipboardDataDataTransfer-like object for this clipboard operation
clipboardData.getData("text")Read plain text from the clipboard (paste handlers)
clipboardData.getData("text/html")Read HTML when available (browser/support dependent)
preventDefault()Cancel the default insert so you can insert manually

⚡ When paste Fires

  • The user pastes via keyboard (Ctrl+V / Cmd+V) or the Edit menu.
  • In editable contexts, the browser prepares to insert clipboard contents at the cursor.
  • A synthetic paste event can be dispatched, but it will not change the document.

⚡ Quick Reference

GoalCode / note
Listenel.addEventListener("paste", fn)
Handler propertyel.onpaste = fn
Read pasted textevent.clipboardData.getData("text")
Transform pastepreventDefault() + insert transformed text yourself
Legacy IE fallbackSome old code used window.clipboardData
MDN statusBaseline Widely available (Jul 2015)

🔍 At a Glance

Four facts to remember about paste.

Event type
ClipboardEvent

clipboardData

Read
getData

Inbound text

Override
preventDefault

Then insert

Baseline
yes

Widely available

Examples Gallery

Examples follow MDN Element: paste event. Copy text somewhere, then paste into the target with Ctrl+V / Cmd+V.

📚 Getting Started

MDN-style transform paste and the handler property.

Example 1 — Uppercase Paste (MDN)

Cancel the default insert, uppercase clipboard text, then insert it at the selection.

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

target.addEventListener("paste", (event) => {
  event.preventDefault();

  let paste = (event.clipboardData || window.clipboardData).getData("text");
  paste = paste.toUpperCase();

  const selection = window.getSelection();
  if (!selection.rangeCount) return;
  selection.deleteFromDocument();
  selection.getRangeAt(0).insertNode(document.createTextNode(paste));
  selection.collapseToEnd();
});
Try It Yourself

How It Works

preventDefault() stops the browser insert; you insert a transformed string with the Selection API.

Example 2 — onpaste Property

Log clipboard length using the handler property form.

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

field.onpaste = (event) => {
  const text = event.clipboardData.getData("text");
  out.textContent = "Pasting " + text.length + " character(s)";
};
Try It Yourself

How It Works

Prefer addEventListener when you need multiple listeners or easy removal.

📈 Log, Trim & Soft Block

Observe pastes, normalize whitespace, or cancel paste for a demo field.

Example 3 — Log When Users Paste

Observe without changing default paste behavior.

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

editor.addEventListener("paste", (event) => {
  const text = event.clipboardData.getData("text");
  out.textContent = "Paste preview: " + JSON.stringify(text.slice(0, 40));
});
Try It Yourself

How It Works

Without preventDefault(), the browser still inserts the original clipboard text.

Example 4 — Trim Pasted Text

Strip leading/trailing whitespace before inserting.

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

target.addEventListener("paste", (event) => {
  event.preventDefault();
  const clean = event.clipboardData.getData("text").trim();
  const selection = window.getSelection();
  if (!selection.rangeCount) return;
  selection.deleteFromDocument();
  selection.getRangeAt(0).insertNode(document.createTextNode(clean));
  selection.collapseToEnd();
});
Try It Yourself

How It Works

Same override pattern as MDN: cancel, transform, insert. Useful for tidy form fields.

Example 5 — Soft-Block Paste

Cancel paste and show a message instead of inserting.

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

locked.addEventListener("paste", (event) => {
  event.preventDefault();
  out.textContent = "Paste is disabled in this field.";
});
Try It Yourself

How It Works

Blocking paste is a UX choice—explain why, and prefer accessible alternatives when possible.

🚀 Common Use Cases

  • Transform pasted text (uppercase, trim, strip formatting).
  • Sanitize paste into rich editors (prefer plain text).
  • Log or audit paste actions in custom editors.
  • Soft-block paste in locked or read-only demo fields.
  • Teach the clipboard trio alongside copy and cut.

🔧 How It Works

1

User pastes

Keyboard shortcut or Edit menu starts a paste action.

UI
2

paste event

ClipboardEvent exposes clipboardData for reading.

Event
3

Default or override

Browser inserts, or your handler preventDefaults and inserts custom data.

Decide
4

Document updates

Editable target shows the pasted (or transformed) content.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Paste handlers can read clipboardData; copy/cut handlers generally write, not read.
  • Synthetic paste events do not change the document contents.
  • Always test in editable contexts (input, textarea, contenteditable).
  • Related learning: copy, cut, addEventListener(), JavaScript hub.

Browser Support

paste 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 paste

Fires on user paste; read clipboardData with getData, or preventDefault to insert transformed text.

Full Widely available
Google Chrome Full support
Yes
Mozilla Firefox Full support
Yes
Apple Safari Full support
Yes
Microsoft Edge Full support
Yes
Opera Full support
Yes
Internet Explorer Supported (legacy clipboardData)
Yes
paste Baseline

Bottom line: Listen for paste to sanitize or transform inbound clipboard text. Pair with copy and cut for the full clipboard story.

Conclusion

paste is the inbound clipboard signal. Read with getData, transform with preventDefault + manual insert, and pair it with copy / cut for complete clipboard UX.

Continue with pointercancel, copy, cut, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use paste to sanitize or transform inbound clipboard text
  • Call preventDefault() when you insert custom paste data
  • Test in real editable fields and contenteditable regions
  • Prefer addEventListener in production code
  • Learn copy and cut alongside paste

❌ Don’t

  • Expect a synthetic paste event to change the document
  • Confuse paste (read) with copy/cut (write)
  • Assume every MIME type is available in every browser
  • Silently block paste without explaining the UX
  • Overwrite onpaste if you need multiple listeners

Key Takeaways

Knowledge Unlocked

Five things to remember about paste

User pasted—read clipboardData or cancel and insert your own.

5
Core concepts
📄02

ClipboardEvent

getData path

API
🔄03

Override

preventDefault

Pattern
👆04

vs copy/cut

inbound read

Compare
🎯05

Baseline

widely available

Status

❓ Frequently Asked Questions

It fires when the user starts a paste action through the browser UI (for example Ctrl+V / Cmd+V or Edit → Paste). In an editable context, the default action inserts clipboard contents at the cursor.
No. MDN marks Element paste 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 read with getData(format) in a paste handler.
Call event.preventDefault() to cancel the default insert, read clipboardData.getData("text"), transform it, then insert the result yourself (for example via the Selection API).
No. You can construct and dispatch a paste event, but MDN notes that doing so will not affect the document's contents.
paste is the inbound path and can read clipboardData. copy and cut are outbound: handlers typically write with setData and cannot read the clipboard.
Did you know?

MDN’s classic paste demo uppercases clipboard text before inserting it. Copy “hello”, paste into the target box, and you get HELLO—proof that paste handlers can reshape inbound data.

Next: pointercancel

Learn the PointerEvent that fires when the browser aborts a pointer.

pointercancel →

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