HTML onpaste Attribute

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

Definition and Usage

The onpaste attribute is an event handler for the paste event. It runs when the user pastes through the browser UI into an editable context — for example an <input>, <textarea>, or contenteditable element.

The event is a ClipboardEvent. Read text with event.clipboardData.getData("text/plain"). To change what is inserted, call event.preventDefault() and insert your own data. Related: oncopy and oncut.

Baseline · Widely available.

Not deprecated. Prefer element.addEventListener("paste", …) in scripts; the HTML attribute is the same handler.

Clipboard reads belong inside the paste handler.

Browsers generally allow clipboardData.getData() only during a user-initiated paste (or copy/cut). A synthetic paste event will not insert clipboard contents by itself.

HTML Syntax

syntax.html
<input onpaste=" handler">

Modern equivalent: el.addEventListener("paste", function (event) { … });. The event bubbles, so a parent can listen too.

Attribute Values

The value is a script (event handler code). Common uses:

UseMeaningTry it
Log / notifyReact when paste happens (default insert still runs)Try it
clipboardDataRead pasted text with getData("text/plain")Try it
preventDefault()Stop default insert; put transformed text yourselfTry it
addEventListenerSame event without the HTML attributeTry it

Live Preview

Focus the field and paste (Ctrl+V / Cmd+V). The log shows length and a short preview of the clipboard text.

Paste here

Default paste still inserts into the field. The handler only reads clipboardData.

Log

Waiting for paste…

HTML Examples

Short demos. Use Try It Yourself, copy some text, then paste into the field.

Example 1 — Log Pasted Text

Read clipboardData without canceling the default insert.

HTML
<label for="msg">Message</label>
<input id="msg" type="text"
  onpaste="document.getElementById('out').textContent =
    'Pasted: ' + event.clipboardData.getData('text/plain')">
<p id="out">Waiting…</p>
Try It Yourself

How It Works

  1. User pastes into the input.
  2. onpaste runs with a ClipboardEvent.
  3. getData("text/plain") reads the clipboard text.
  4. Default paste still inserts unless you call preventDefault().

Example 2 — Paste as Uppercase

Cancel the default insert, then put a transformed value.

HTML
<label for="code">Code</label>
<input id="code" type="text"
  onpaste="
    event.preventDefault();
    var text = event.clipboardData.getData('text/plain').toUpperCase();
    var el = event.target;
    var start = el.selectionStart;
    var end = el.selectionEnd;
    el.value = el.value.slice(0, start) + text + el.value.slice(end);
    el.setSelectionRange(start + text.length, start + text.length);
  ">
Try It Yourself

How It Works

  1. preventDefault() stops the browser’s normal paste.
  2. getData reads the clipboard string.
  3. toUpperCase() transforms it.
  4. Manual insert at the caret keeps editing natural.

Example 3 — addEventListener("paste")

Same event without the HTML attribute — preferred in modern scripts.

HTML
<label for="t">Notes</label>
<textarea id="t" rows="3"></textarea>
<p id="status">Waiting…</p>
<script>
  document.getElementById("t").addEventListener("paste", function (event) {
    var text = event.clipboardData.getData("text/plain");
    document.getElementById("status").textContent =
      "Pasted " + text.length + " characters.";
  });
</script>
Try It Yourself

How It Works

  1. Listener attaches to the textarea.
  2. User pastes — the handler runs.
  3. Status shows how many characters were on the clipboard.
  4. Default paste still fills the textarea.

Edge Cases & Pitfalls

Most issues are clipboard permissions, canceling without re-inserting, or blocking paste for everyone.

timing

Read clipboard only in the handler

getData() is available during the paste event. Do not expect free clipboard access afterward.

cancel

If you preventDefault(), insert yourself

Otherwise nothing appears in the field. Transform, then write the value or selection.

a11y

Avoid blocking paste without a good reason

Password managers and assistive tech rely on paste. Prefer validate-after-paste over hard blocks.

fake

Synthetic paste does not insert

Dispatching a paste event will not put clipboard data into the document.

types

MIME types vary

Prefer text/plain. Rich paste may also offer text/html depending on the source.

Browser Support

onpaste is an HTML event attribute for the Clipboard API paste event. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

onpaste

Supported across modern browsers for user-initiated paste into editable content. Clipboard access rules still apply.

97% Global support (approx.)
Google Chrome Yes
Full support
Mozilla Firefox Yes
Full support
Apple Safari Yes
Full support
Microsoft Edge Yes
Full support
Opera Yes
Full support
Samsung Internet Yes
Full support
Internet Explorer Yes*
Legacy clipboardData
onpaste Widely supported

Bottom line: Safe to use for paste handling in forms and editors. Read clipboardData during the event; cancel only when you replace the insert yourself.

Key Takeaways

  • Role: run code when the user pastes into editable content.
  • Read with event.clipboardData.getData("text/plain").
  • Transform: preventDefault(), then insert your text.
  • Do not block paste lightly — it hurts accessibility and UX.

One line: onpaste lets you inspect or reshape clipboard text when the user pastes.

Frequently Asked Questions

It is an event handler attribute for the paste event. When the user pastes into an editable control (or a bubbling target), your script runs. You can read event.clipboardData and optionally cancel the default insert.
Inside the paste handler, call event.clipboardData.getData("text/plain") (or "text"). Outside a user-initiated clipboard event, browsers usually block clipboard reads.
Call event.preventDefault(), then insert your own text (for example set the input value or insert a text node into a contenteditable selection).
No. It is Baseline widely available. Prefer element.addEventListener("paste", …) in modern scripts; the HTML attribute is the same handler.
No. Dispatching a fake paste event does not change the document. Only a real user paste (or your own insert after preventDefault) updates content.
On editable elements such as input, textarea, and contenteditable regions, or on ancestors that listen while the event bubbles.

Next: onpause

Run code when media playback is paused.

onpause 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