HTML onpaste Event Attribute

If this site has been useful, we’d love your support! Running this site takes time and resources, and every small contribution helps us keep creating valuable content. Consider buying us a coffee to keep things going strong!

Description

The onpaste HTML event attribute specifies the JavaScript function to be executed when the user pastes content into an element. It is supported by all HTML elements, but can only be used on elements that are editable, such as <input>, <textarea>, and elements with the contenteditable attribute set to true.

When the user pastes content into an element with the onpaste attribute, the JavaScript function specified by the attribute is executed. The function can then access the clipboard contents using the clipboardData property of the event object.

The onpaste event attribute can be used for a variety of purposes, such as:

  • Validating the pasted content before it is inserted into the element.
  • Converting the pasted content to a different format.
  • Performing some other action, such as updating a database or sending an email.

Here is an example of how to use the onpaste event attribute:

<input type="text" onpaste="validatePastedContent(event)">

The validatePastedContent() function would be responsible for validating the pasted content before it is inserted into the <input> element. If the content is not valid, the function could prevent it from being inserted or display an error message to the user.

Syntax

<element onpaste="script">

Values

  • scriptThe name of the script to use when the event has been triggered.

Example

<!DOCTYPE html> 
<html>
<head>
<title>onpaste event</title>
</head>
<body>
<textarea onpaste="pasteAlarm()">Paste text here.</textarea>
<p id="test"> </p>
<script>
function pasteAlarm() {
document.getElementById("test").innerHTML = "The onpaste attribute has been triggered.";
}
</script>
</body>
</html>

Browser Support

The following table will show you the current browser support for the HTML onpaste Event Attribute.

Desktop
Edge Chrome Firefox Opera Safari
YesYesYesYesYes
Tablets / Mobile
Chrome Firefox Opera Safari Samsung Webview
YesYesYesYesYesYes

Last updated by CSSPortal on: 13th October 2023