<p class="to-copy">Chuck ipsum. Although it is not common knowledge, there are actually three sides to the Force: the light side, the dark side, and Chuck Norris.</p>
<button class="copy-btn" type="button">Copy!</button>
<br />
<br />
<div>
<div>
<label for="paste">Paste!</label>
</div>
<textarea name="paste" id="paste" cols="30" rows="10"></textarea>
</div>
JavaScript
var $toCopy = $('.to-copy')
var $copyBtn = $('.copy-btn');
$copyBtn.on('click', function (e) {
var target = $toCopy[0];
var range = document.createRange();
var selection = window.getSelection();
var selectedText;
selection.removeAllRanges();
range.selectNodeContents(target);
selection.addRange(range);
selectedText = selection.toString();
console.log(selectedText);
try {
if (!document.execCommand('copy')) {
alert('copy did not succeed');
}
} catch (e) {
alert('this browser does not support this feature');
}
});
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.