This is how I fixed the problem while working on it until late at 4:00 and posted my problem ( click here to see), hope it will help someone not burn their night :) I tried to do what it does current Bootstrap site for copying code, but Bootstrap currently uses the old zeroClipboard plugin. I tried to do the same thing as Bootstrap, but with the latest zeroClipboard (for now).
HTML: Please note that I did not add all the code in one line, I do not bring pre and code in the second line or indentation, so that the code looks good, it will be copied to the clip.
<div class="highlight"><pre><code>Some code/text goes here</code></pre></div>
CSS
.zero-clipboard { position: relative; display: none; } .btn-clipboard { position: absolute; top: 0; right: 0; z-index: 10; display: block; padding: 5px 8px; font-size: 12px; color: #777; cursor: pointer; background-color: #fff; border: 1px solid #e1e1e8; border-radius: 0 4px 0 4px; } .btn-clipboard-hover { color: #fff; background-color: #563d7c; border-color: #563d7c; } @media (min-width: 768px) { .zero-clipboard { display: block; } .zero-clipboard .btn-clipboard { top: -16px; border-top-right-radius: 0; } }
JavaScript: place ZeroClipboard.swf where the JS file is located.
// Config ZeroClipboard ZeroClipboard.config({ hoverClass: 'btn-clipboard-hover' }) // Insert copy to clipboard button before .highlight $('.highlight').each(function () { var btnHtml = '<div class="zero-clipboard"><span class="btn-clipboard">Copy</span></div>'; $(this).before(btnHtml) }); var zeroClipboard = new ZeroClipboard($('.btn-clipboard')); var htmlBridge = $('#global-zeroclipboard-html-bridge'); // Handlers for ZeroClipboard zeroClipboard.on('ready', function (event) { htmlBridge .data('placement', 'top') .attr('title', 'Copy to clipboard') .tooltip(); // Copy to clipboard zeroClipboard.on('copy', function (event) { var highlight = $(event.target).parent().nextAll('.highlight').first(); event.clipboardData.setData("text/plain", highlight.text()) }); // Notify copy success and reset tooltip title zeroClipboard.on('aftercopy', function () { htmlBridge .attr('title', 'Copied!') .tooltip('fixTitle') .tooltip('show') .attr('title', 'Copy to clipboard') .tooltip('fixTitle') }); }); // Notify copy failure zeroClipboard.on('error', function () { ZeroClipboard.destroy(); htmlBridge .attr('title', 'Flash required') .tooltip('fixTitle') .tooltip('show'); });
Syed
source share