• I believe we can't do it with just javascript, what we usually see around is a plugin that puts flash obj to do the work, see more info here: https://github.com/blog/1365-a-more-transparent-clipboard-button – G.Mendes Jul 18 '14 at 20:50
  • 6
    Users complain. Doesn't mean you have to cater to every whim. – j08691 Jul 18 '14 at 20:52
  • It is not about the _why_ but the _how_ guys. Stop the critics. – morkro Jul 18 '14 at 20:54
  • +1 for cool jsfiddle example. – Scherbius.com Jul 18 '14 at 21:00
  • btw, cool guys use FireBug to extract values from unselectable HTML elements :P – Scherbius.com Jul 18 '14 at 21:01
  • You can see the following blog which will help you to save the content to the clipboard: http://www.deluxeblogtips.com/2010/06/javascript-copy-to-clipboard.html. For IE, use the clipboardData.setData('text', s); method to save the content. For the other browsers use the flash based solution. – Rumen Jekov Jul 18 '14 at 21:15
  • 1 Answers1

    1

    I think it cannot be done with a button (like in your example). However it is somewhat possible by doing something like described below.

    First we need a textfield, which value is editable and can be selected (this will work as our "clipboard"). A second thing we need is a way to detect if user presses CTRL (for CTRL+C). So the basic idea is to copy selected values to our textfield and when the user presses CTRL we select the contents of our textfield. Then by pressing C the user is performing a copy command on our textfield instead of the select-element.

    Here's a basic implementation (check the jsfiddle below). You can fine tune it to match your needs :)

    HTML

    <select multiple="multiple" size="10" id="selection" onkeydown="keydown(event)" onchange="changeClipboardValue(this)" >
    <option value="test1">Test1</option> <option value="test2">Test2</option> </select> <input type="text" id="clipboard" onkeyup="keyup(event)" />

    JavaScript

    function changeClipboardValue(selectBox) { var clipboard = document.getElementById("clipboard"); var text = ""; for (i = 0; i < selectBox.length; i++) { if(selectBox.options[i].selected) text += selectBox.options[i].value + ","; } clipboard.value = text; }

    function keydown(e) { if(e.keyCode === 17) { var clipboard = document.getElementById("clipboard"); clipboard.select(); } }

    function keyup(e) { if(e.keyCode === 17) { var selectBox = document.getElementById("selection"); selectBox.focus(); } }

    Might want to add CSS to hide the clipboard field #clipboard {width: 1px;height: 1px;padding:0;position:absolute;left:-9999px;}

    http://jsfiddle.net/LubZt/

    UPDATE: http://jsfiddle.net/Kcv6j/ this version works better when holding CTRL to select multiple items.

    niklas-e
    • 418
    • 5
    • 11
    • This is a good hack, it's a bit buggy though if a user selects multiple items (holding ctrl) then presses c it will not work. The user has to release ctrl then press it again in order to copy. Other that its good thinking. – Aaron Jul 22 '14 at 15:47
    • Hmm, holding ctrl to select multiple works for me. What browser are you using Aaron? – niklas-e Jul 22 '14 at 15:56
    • 1
      I managed to reproduce the problem you described... Can be fixed with something like this http://jsfiddle.net/Kcv6j/ – niklas-e Jul 22 '14 at 16:19