Closed KuroNoDev closed 8 years ago
Unfortunately this isn't possible due to browser security policies -- copying to clipboard has to be from a user-initiated action (e.g., a click event). Or I could be wrong.
This should be possible by creating a hidden input text and updating its value for every api response (assuming the user enabled auto-copying option), focus then select, then copy it to clipboard via document.execCommand('Copy');
Yeah, that's how I'm doing the copying now. The document.execCommand('copy')
doesn't work unless it's inside an event handler: It'd be alarming if a website could copy arbitrary text without user interaction.
<textarea id="id1">this doesn't get copied</textarea>
<textarea id="id2">this gets copied</textarea>
<button>Copy id2</button>
<script>
var div1 = document.querySelector('#id1');
div1.select();
document.execCommand('copy'); // Shouldn't work
var div2 = document.querySelector('#id2');
var button = document.querySelector('button');
button.onclick = function(e) {
div2.select();
document.execCommand('copy'); // Should work
};
</script>
(codepen)
Does that work for you? id1
doesn't get copied for me, id2
gets copied if I click.
aahh tried to also trigger click automatically but it also didn't work. I apologize for the waste of time (that's actually irresponsible of me to not try it first).
No worries!
Maybe a checkbox on each column