How to execute ctrl + c or copy commad using javascript or jquery when button is clicked

Can I execute the copy command with the EVENT click?

I have selected text and I want this text to be copied to the event onClick, so that I can skip this text to another page using right click or CTRL+ Cto copy the text.

+4
source share
3 answers
function copyText(){
    var txt = '';
     if (window.getSelection)
        txt = window.getSelection();
    else if (document.getSelection)
        txt = document.getSelection();
    else return;
    document.getElementById("a").value=txt;
    allCopied =document.getElementById("a").createTextRange();
    allCopied.execCommand("RemoveFormat");

   allCopied.execCommand("Copy");
}

but for security reasons, most browsers do not allow you to change the clipboard ( except Internet Explorer) .

+2
source

HTML

<form name="myForm">
<span onclick="copyText(this)" >Text1</span>, <span onclick="copyText(this)" >Text2</span>
<br>
<input name="myField"></input>

Javascript

function copyText(element) {
document.myForm.myField.value = element.innerHTML;
}

- Ctrl + C

$("#text1").click(function(){
var holdtext = $("#clipboard").innerText;
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");
});
0

use getselection () to get the selected text inside the browser window.

0
source

Source: https://habr.com/ru/post/1532639/


All Articles