<p>
This is what I need, but it doesn't work on Safari
</p>
<select onchange="eventHandler(this.value);"><option value="0" class="hidden" selected="true" disabled="true">Menu</option><option value="Text I want to copy">Copy to clipboard</option></select>
<p>
This one works on Safari
</p>
<button onclick="eventHandler('Text I want to copy #2');">Test with button</button>
JavaScript
function eventHandler(value) {
writeTextToClipboard(value);
}
async function writeTextToClipboard(value) {
// Safari, Chrome
if(typeof ClipboardItem && navigator.clipboard.write) {
const type = 'text/plain'
const blob = new Blob([value], {type})
const cbi = new ClipboardItem({[type]: blob})
await navigator.clipboard.write([cbi])
console.log(`Copied: ${value}`)
}
// Firefox
else {
navigator.clipboard.writeText(value);
}
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.