JavaScript - Detect text copy operation
A JavaScript code example for detecting text copy operations.
The HTML has a paragraph with class "text" to detect copies from.
<div class="item-container">
<p id="text">Please copy this text.</p>
<p id="output">***</p>
</div>
An event triggered on copy is implemented on the target element with addEventListener()
.
// Implement copy triggered event
document.querySelector('#text').addEventListener('copy', () => {
output.textContent = 'Copied text.';
});