HTML onpaste Attribute

HTML onpaste Attribute: This attribute is defined as it fires when the user passes some content in an element. This event attribute is supported by all HTML elements. It is mostly used with <input> element.

There are three ways to paste the content in an HTML element which are listed below:

  • Use CTRL + V key
  • Select “Paste” from the edit menu in the browser.
  • select the “Paste” command from right click menu

HTML onpaste Attribute

This attribute can be applied to all the HTML elements.

Attribute Value: The script event run when the onpaste attribute is called.

Syntax: <element onpaste = “script”>

Browser Support:

This attribute is supported by the following browsers:

  • Firefox
  • Chrome
  • Internet Explorer
  • Safari
  • Opera

Example: for <input> element

<!DOCTYPE html>
<html>
<body>
<input type="text" onpaste="myFunction()" value="Try to paste something in here" size="40">
<p id="demo"></p>
<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "You pasted text!";
}
</script>
</body>
</html>

Output:

HTML onpaste attribute

Example: for <p> element

<!DOCTYPE html>
<html>
<body>
<p contenteditable="true" onpaste="myFunction()">Try to paste something inside this paragraph.</p>
<script>
function myFunction() {
  alert("You pasted text!");
}
</script>
</body>
</html>

Output:

Try to paste something inside this paragraph.