Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Which event occurs in JavaScript when an element is content is copied to clipboard?
When a user copies an element's content, the oncopy event triggers in JavaScript. This event fires when the user performs a copy operation using Ctrl+C, right-click copy, or any other method to copy selected content.
Syntax
element.oncopy = function() {
// Code to execute when content is copied
};
// Or using addEventListener
element.addEventListener('copy', function() {
// Code to execute when content is copied
});
Example: Basic oncopy Event
<!DOCTYPE html>
<html>
<body>
<input type="text" id="textInput" value="Copy this text" />
<p id="message"></p>
<script>
function copyFunction() {
document.getElementById("message").innerHTML = "Text copied to clipboard!";
}
document.getElementById("textInput").oncopy = copyFunction;
</script>
</body>
</html>
Example: Using addEventListener Method
<!DOCTYPE html>
<html>
<body>
<textarea id="content" rows="3" cols="30">Select and copy this content</textarea>
<div id="status"></div>
<script>
document.getElementById("content").addEventListener('copy', function() {
document.getElementById("status").innerHTML = "Content copied at: " + new Date().toLocaleTimeString();
});
</script>
</body>
</html>
Key Points
- The oncopy event works with text inputs, textareas, and any selectable content
- It triggers after the content is successfully copied to clipboard
- Can be used to track copy operations or provide user feedback
- Works with keyboard shortcuts (Ctrl+C) and context menu copy
Browser Compatibility
The oncopy event is supported in all modern browsers including Chrome, Firefox, Safari, and Edge. It's part of the standard DOM events specification.
Conclusion
The oncopy event provides a simple way to detect when users copy content from your web page. Use it to enhance user experience by providing feedback or tracking copy operations.
Advertisements
