onpasteEvent

事件对象参考手册Event Object

Examples

Execute JavaScript when pasting text on an <input> element:

<input type="text" onpaste="myFunction()" value="Try pasting text here">

Try it Yourself »

More examples can be found at the bottom of this article.


Definition and Usage

The onpaste event is triggered when the user pastes text into an element.

Note:Although the HTML elements used all support the onpaste event, not all elements actually support it, such as the <p> element, unless contenteditable is set to "true" (see more examples below).

Tip:The onpaste event is usually used for <input> elements with type="text".

Tip:There are three ways to paste content into an element:

  • Press CTRL + V
  • Select "Paste" from the browser's edit menu
  • Right-click the mouse button and select the "Paste" command from the context menu.

Browser Support

Event
onpaste Yes Yes Yes Yes Yes


Syntax

In HTML:

<element onpaste="myScript">Try it Yourself

In JavaScript:

object.onpaste=function(){myScript};Try it Yourself

In JavaScript, using the addEventListener() method:

object.addEventListener("paste", myScript);Try it Yourself

Note:Internet Explorer 8 and earlier IE versions do not supportaddEventListener()method.


Technical Details
Bubbles: Yes
Cancelable: Yes
Event type: ClipboardEvent
Supported HTML tags: All HTML elements


Examples

More Examples

Example

Execute JavaScript when text is pasted onto a <p> element (note contenteditable is set to "true"):

<p contenteditable="true" onpaste="myFunction()">Try pasting content into the paragraph.</p>

Try it Yourself »


Related Pages

HTML DOM Reference:oncopy event

HTML DOM Reference:oncut event


事件对象参考手册Event Object Other Extensions