I am using the Clipboard.js
plugin a website, and stuck with the issue selecting only parent class of the .copy
button on-click.
The problem is that I want the code inside pre
tag to be copied with rich text formatting and the method I am using does it well (return document.querySelector("myClass")
), however when I am changing the preceding code with the one I found in a similar question (return $(trigger).closest(".fw-code-copy").next("code").text();
) I can copy each block's code but loosing formatting, I mean the code is being copied as plain text.
Could you please review the code I have and advise how to find the class of button's parent?
HTML
<div class="code-snippet">
<pre class="code">
<div>
some code
</div>
</pre>
<input class="copy" type="button" value="copy">
</div>
<div class="code-snippet">
<pre class="code">
<div>
some other code
</div>
</pre>
<input class="copy" type="button" value="copy">
</div>
CSS
.code-snippet{
position: relative;
width: 100%;
bordeR: 1px solid red;
margin-bottom: 20px;
}
.copy{
position: absolute;
right: 20px;
bottom: 20px;
}
JS
$(document).ready(function(){
var clipboard = new Clipboard('.copy', {
target: function() {
return document.querySelector('.code');
}
});
});