How to copying data to memory with JavaScript and jQuery? Read and understand.
Turns out that a simple solution can be achieved using JavaScript and jQuery. Let's see how.
A Simpler Approach
Here's how I approached it:1. add a class to show a copy icon whenever you hover a DOM element:
Ex. class="icon-clipboard"2. add a css class to identify what you want to copy:
Ex. class="copyableTemplate"3. add the data to copy using the "data-clipboard" attribute:
Ex. data-clipboard="http://lipsum.com/"4. handle the click event on the copy icon and get the data associated to it:
Ex. $('body').on('click', 'div.icon-clipboard', function() { })5. copy to memory using the browser's api:
Ex. document.execCommand('copy');Source Code:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script> | |
<style> | |
div.copyableTemplate div{ | |
display:none; | |
} | |
div.copyableTemplate:hover div{ | |
display:inline; | |
} | |
.icon-clipboard:before { | |
content: " Copy URL... "; | |
cursor: pointer; | |
font-size: 0.9em; | |
background-color: crimson; | |
color: white; | |
padding: 3px; | |
border-radius: 10px; | |
} | |
</style> | |
<script> | |
$(document).ready(function(){ | |
// Init Clipboard | |
var initClipboard = function() { | |
$('body').on('click', 'div.icon-clipboard', function() { | |
console.log('onClick'); | |
var data = $(this).data('clipboard'); | |
$('#clipboard').show(); | |
$('#clipboard').val(data); | |
if (data) { | |
$('#clipboard')[0].select(); | |
} | |
// try copying to memory | |
try { | |
document.execCommand('copy'); | |
alert('Value: "' + data + '" copied to memory!'); | |
} catch (err) { | |
console.log('Error copying data to clipboard. Please check if your browser supports this feature..'); | |
} | |
// need to show/hide else, textarea loses value | |
$('#clipboard').hide(); | |
}); | |
} | |
initClipboard(); | |
}); | |
</script> | |
</head> | |
<body> | |
<h1>Copy data using Javascript and jQuery</h1> | |
<textarea id="clipboard" style="display:none;"></textarea> | |
<div class="copyableTemplate"> | |
Quisque eleifend sapien sed interdum fermentum. Sed suscipit nibh massa, in maximus nulla imperdiet vitae. | |
<div class="icon-clipboard" data-clipboard="http://lipsum.com/" title="Click to copy the Id to the clipboard"></div> | |
</div> | |
<p style="padding-top:20px"> | |
Hope it helps!<br> | |
Bruno Hildenbrand | |
</p> | |
</body> | |
</html> |
See Also
- My journey to 1 million articles read
- JavaScript caching best practices with ASP.NET
- Testing JavaScript on ASP.NET Core Applications
- Hosting NuGet packages on GitHub
- Why use .NET Core
- Package Management in .NET Core
- Configuration in .NET Core console applications
- Exporting HTML to PDF using JavaScript
- Building and Running ASP.NET Core apps on Linux
- Generating views in the backend with ASP.NET
- Importing CSVs with .NET Core and C#
- Exporting a CSV generated in-memory in ASP.NET with C#