1

I need to be able to click a link and have the following copied to my clipboard:

Name Email [TAB] Address Phone [TAB] IP Address

How can I "copy" the tab key? And how do I add it to clipboard?

invidious
  • 241
  • 1
  • 5
  • 14
  • Have you tried to find a solution on your own before asking? – Anonymous May 22 '15 at 22:22
  • possible duplicate of [How do I copy to the clipboard in JavaScript?](http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript) – joews May 23 '15 at 11:48

2 Answers2

0

Here's a pure JavaScript solution. It works in Chrome 43+ and Internet Explorer. You'll need to use Flash or a library for cross-browser support.

You can use document.execCommand('copy') to copy a message from a (hidden) element onto the clipboard.

Clicking the button will copy a message to the clipboard:

var cutTextareaBtn = document.querySelector('.js-textareacutbtn');
var cutTextarea = document.querySelector('.js-cuttextarea');  

cutTextarea.textContent = "Name Email\tAddress Phone\tIP Address";

cutTextareaBtn.addEventListener('click', function(event) { 
  cutTextarea.select();

  try {  
    var successful = document.execCommand('copy');  
    var msg = successful ? 'successful' : 'unsuccessful';  
    console.log('Copy text command was ' + msg);  
  } catch(err) {  
    console.log('Oops, unable to copy');  
  }  
});
<textarea class="js-cuttextarea">fdsfsd</textarea>
<button class="js-textareacutbtn">Copy to clipboard</button>

Code adapted from this HTML5 rocks article.

This works in Chrome 43+ and Internet Explorer, though support for other browsers is limited - see caniuse.com.

joews
  • 29,767
  • 10
  • 79
  • 91
  • There's a proper library to do this. – Nizar B. May 23 '15 at 08:02
  • Using a library isn't more "proper" than using a built-in Web platform feature, but it definitely has advantages in compatibility and ease of use. Why the downvote, though? This answers the question, and makes the limited browser support clear. – joews May 23 '15 at 11:45
  • No way to remove because it's true, you need to test before to talk like that. – Nizar B. May 23 '15 at 12:13
-1

I think it's not possible with Javascript to do a copy/paste to the clipboard feature. You need Flash script to do that. Or, you may use this library but you'll still use Adobe flash: https://github.com/zeroclipboard/zeroclipboard

Nizar B.
  • 3,098
  • 9
  • 38
  • 56
  • It is possible - Chrome 43 released the feature a few days ago, and it's possible in other browsers with some caveats. See http://updates.html5rocks.com/2015/04/cut-and-copy-commands and http://caniuse.com/#feat=clipboard. – joews May 22 '15 at 22:34
  • The downvote was for "it's not possible in JavaScript", because it is now possible in many browsers. I'll remove it if you edit. – joews May 23 '15 at 11:46
  • Second thing, you're code snippet doesn't work, it doesn't copy the string content to the clipboard at all! – Nizar B. May 23 '15 at 12:19
  • It does work in Chrome 43 (tested on OSX and Android), and should work in IE. It won't work in other browsers. I'll edit to make that clearer. – joews May 23 '15 at 12:45