Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy values from html <select multiple> to clipboard

I have this select element

<select multiple="multiple" size="10" id="selection">  
    <option value="test1">Test1</option> 
    <option value="test2">Test2</option>
</select>

A user is complaining because they cannot copy values to their clipboard. Is there any way to make it so users can copy? I created this JSfiddle however it is not a good solution in my opinion.

http://jsfiddle.net/22gy9/4/

like image 799
Aaron Avatar asked Jul 18 '14 20:07

Aaron


People also ask

How do you select multiple values in HTML?

When present, it specifies that multiple options can be selected at once. Selecting multiple options vary in different operating systems and browsers: For windows: Hold down the control (ctrl) button to select multiple options. For Mac: Hold down the command button to select multiple options.

How do I copy something to the clipboard in HTML?

writeText() function. This function writes the data fed to it as a parameter into the clipboard. We can use this to copy any text to the clipboard. First, we select the text to copy to the clipboard whether it is from a div or from an input box using document.

How do I copy all text in HTML?

Copy the entire code by highlighting all or just the specific area of code that you want, pressing Ctrl+C or Command+C on your keyboard and then paste the code into a text or document file.

How do you copy text from a tag in HTML?

Tip: The oncopy attribute is mostly used on <input> elements with type="text" . Tip: There are three ways to copy an element/the content of an element: Press CTRL + C. Select "Copy" from the Edit menu in your browser.


1 Answers

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.

like image 126
niklas-e Avatar answered Sep 30 '22 18:09

niklas-e