JSFiddle - React, Tailwind, and code Playground
HTML
<select id="A">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
JavaScript
//to be ran on keydown, which occurs before clipboard copy
function copyWatch(e) {
e = e || event;
if (
//not ctrl+C
(!(e.ctrlKey && e.keyCode == '67')) ||
//nothing selected
(this.selectedIndex < 0)
)
return;
//create selectable text
var copyEl = document.createElement('textarea');
copyEl.innerHTML = this.options[this.selectedIndex].innerHTML;
//hide it, but in a way the browser thinks is clickable
//(no visibility:hidden, display:none)
copyEl.style.position = 'absolute';
copyEl.style.left = '-9999px';
var that = this;
//add a call back for after the ctrl+c is completed
copyEl.onkeyup = function() {
//remove the extraneous element
copyEl.parentNode.removeChild(copyEl);
//return focus to the select
that.focus();
};
//add it to the document, and highlight all the text in the textarea,
//ready for the ctrl+c copy event to fire
document.body.appendChild(copyEl).select();
}
document.getElementById('A').onkeydown = copyWatch;