HTML onpaste Event Attribute

The HTML onpaste attribute is an event handler that triggers when a user pastes content into an HTML element. This event occurs when content is pasted using Ctrl+V, right-click paste, or any other paste operation.

Syntax

Following is the syntax for the onpaste attribute −

<tagname onpaste="script">Content</tagname>

Where script is the JavaScript code to execute when the paste event occurs.

Parameters

  • script − JavaScript code or function to execute when content is pasted into the element.

Commonly Used Elements

The onpaste attribute is most commonly used with form elements that accept text input −

  • <input> elements (text, password, email, etc.)

  • <textarea> elements

  • Elements with contenteditable="true" attribute

Basic Example

Following example demonstrates the onpaste attribute with a textarea element −

<!DOCTYPE html>
<html>
<head>
   <title>HTML onpaste Event Attribute</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>onpaste Event Demo</h2>
   <p>Copy this text: Hello World!</p>
   <textarea placeholder="Paste text here" onpaste="showAlert()" rows="4" cols="40"></textarea>
   <p id="message"></p>
   
   <script>
      function showAlert() {
         document.getElementById('message').innerHTML = "Content pasted!";
      }
   </script>
</body>
</html>

When you paste content into the textarea, the message "Content pasted!" appears below it −

onpaste Event Demo
Copy this text: Hello World!
[Textarea with pasted content]
Content pasted!

Accessing Paste Event Data

You can access the pasted content using the event object's clipboardData property. This allows you to validate or modify the pasted content before it's inserted.

Example − Reading Pasted Content

<!DOCTYPE html>
<html>
<head>
   <title>Reading Pasted Content</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Paste Content Reader</h2>
   <input type="text" placeholder="Paste something here" onpaste="readPaste(event)" style="width: 300px; padding: 5px;">
   <p><strong>Pasted content:</strong> <span id="content"></span></p>
   
   <script>
      function readPaste(event) {
         setTimeout(function() {
            var pastedText = event.target.value;
            document.getElementById('content').textContent = pastedText;
         }, 10);
      }
   </script>
</body>
</html>

This example displays the pasted content in real-time below the input field.

Preventing Default Paste Behavior

You can prevent the default paste operation and implement custom logic by using event.preventDefault().

Example − Custom Paste Handling

<!DOCTYPE html>
<html>
<head>
   <title>Custom Paste Handler</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Numbers Only Input</h2>
   <p>Try pasting text or numbers:</p>
   <input type="text" placeholder="Only numbers allowed" onpaste="numbersOnly(event)" style="padding: 5px;">
   <p id="status"></p>
   
   <script>
      function numbersOnly(event) {
         event.preventDefault();
         var paste = (event.clipboardData || window.clipboardData).getData('text');
         var numbersOnly = paste.replace(/[^0-9]/g, '');
         
         if (numbersOnly !== paste) {
            document.getElementById('status').innerHTML = 'Non-numeric characters removed!';
            document.getElementById('status').style.color = 'red';
         } else {
            document.getElementById('status').innerHTML = 'Numbers pasted successfully!';
            document.getElementById('status').style.color = 'green';
         }
         
         event.target.value = numbersOnly;
      }
   </script>
</body>
</html>

This example filters out non-numeric characters from pasted content and shows a status message.

Using with Contenteditable Elements

The onpaste attribute also works with contenteditable elements for rich text editing scenarios.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Contenteditable Paste Event</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Editable Content Area</h2>
   <div 
        onpaste="trackPaste()" 
        style="border: 2px solid #ccc; padding: 10px; min-height: 100px; background: #f9f9f9;">
      Click here and paste some content...
   </div>
   <p>Paste operations: <span id="counter">0</span></p>
   
   <script>
      var pasteCount = 0;
      function trackPaste() {
         pasteCount++;
         document.getElementById('counter').textContent = pasteCount;
      }
   </script>
</body>
</html>

This example tracks the number of paste operations performed in a contenteditable div.

onpaste Event Flow User Pastes onpaste Fires Script Executes Content Inserted Common Use Cases: ? Input validation ? Content formatting ? Paste tracking ? Data transformation

Event Object Properties

The paste event provides access to clipboard data through the following properties −

Property Description
event.clipboardData Contains the clipboard data being pasted
event.target The element receiving the pasted content
event.preventDefault() Prevents the default paste behavior
clipboardData.getData('text') Retrieves text content from the clipboard

Browser Compatibility

The onpaste attribute is supported in all modern browsers including Chrome, Firefox, Safari, Edge, and Internet Explorer 9+. However, accessing clipboardData may have different implementations across browsers.

Conclusion

The HTML onpaste attribute provides a powerful way to handle paste operations in web forms and editable content. It enables input validation, content transformation, and user interaction tracking whenever users paste content into HTML elements.

Updated on: 2026-03-16T21:38:54+05:30

294 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements