onpaste Event
Example
Execute a JavaScript when pasting some text in an <input> element:
<input type="text" onpaste="myFunction()" value="Paste something here">
Try it Yourself »
More examples below
Definition and Usage
The onpaste
event occurs when the user pastes some content into an element.
The onpaste
event is mostly used on <input> elements with type="text".
Note
It is only possible to paste something into an input field.
It is not possible to paste content into, for example a <p> element, UNLESS the element has set contenteditable to "true" (See "More Examples" below).
3 Ways to Paste:
- Press Ctrl + V
- Select "Paste" from the Edit menu in your browser
- Right click to display the context menu, and select "Paste"
Clipboard Events
Event | Description | Bubbles |
---|---|---|
copy | The user copies content | Yes |
cut | The user starts cuts content | Yes |
paste | The user pastes content | Yes |
See Also:
Syntax
In JavaScript, using the addEventListener() method:
object.addEventListener("paste", myScript);
Try it Yourself »
Technical Details
Bubbles: | Yes |
---|---|
Cancelable: | Yes |
Event type: | ClipboardEvent |
Supported HTML tags: | All HTML elements |
More Examples
Example
Execute a JavaScript when pasting some text in a <p> element.
(Note that contenteditable is set to "true"):
<p contenteditable="true" onpaste="myFunction()">Try to paste something inside this paragraph.</p>
Try it Yourself »
Browser Support
onpaste
is a DOM Level 3 (2004) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |