As of 2016, you can now copy text to the clipboard in most browsers (everywhere except Safari) because most browsers have the ability to pro-grammatically copy a selection of text to the clipboard using document.execCommand("copy") that works off a selection.
As with some other actions in a browser (like opening a new window), the copy to clipboard can only be done via a specific user action (like a mouse click). For example, it cannot be done via a timer.
Here's a code example:
HTML
jQuery
As with some other actions in a browser (like opening a new window), the copy to clipboard can only be done via a specific user action (like a mouse click). For example, it cannot be done via a timer.
Here's a code example:
HTML
<div class="showCodeWrapper">
<btn class="btn btn-default copyMe">
<i class="fa fa-clipboard"></i>
Copy
</btn>
<textarea data-app-type="countdown-timer" class="form-control embedShortcode showCode selectAll" readonly="">test</textarea>
</div>
jQuery
<script>
$( "li.third-item" ).siblings().css( "background-color", "green" );
$(document).on("click", ".copyMe", function() {
var e = $(this).siblings("textarea")[0];
e.select();
try {
if (!document.execCommand("copy")) throw "Copy unsuccessful";
$(this).hide().html('<i class="fa fa-check"></i> Copied').fadeIn("fast")
} catch (t) {
$(this).hide().html("Press ⌘+C to copy.").fadeIn("fast"), debug("Copying text error " + t)
}
})
</script>
No comments:
Post a Comment