?>

How to copy javascript text to clipboard?

How to copy javascript text to clipboard?

To copy any text into js clipboard, you can paste this text into input, which must be visible and not in a twisted div, select it and use the built-in js document.exe function ("copy");

Html

<i class="fas fa-copy sx-copy" style="cursor: pointer;" data-toggle="tooltip" title="Скопировать ссылку"></i> <input id="cont" type="text" value="Любой ваш текст" style="position: absolute; left: -20000px;"/> 

Please note that the input is visible but in order to be not visible it uses absolute positioning -20000px

Js

$(".sx-copy").on("click", function() { var input = document.getElementById("cont"); // Select the input node's contents input.select(); // Copy it to the clipboard _.delay(function() { try { // Теперь, когда мы выбрали текст ссылки, выполним команду копирования var successful = document.execCommand("copy"); var msg = successful ? 'successful' : 'unsuccessful'; sx.notify.success("Ссылка скопирована"); } catch(err) { throw err; sx.notify.error('Oops, unable to copy'); } }, 300); }); 

All Comments (0)
No Comments