I've found this great code from plumwd, which I really love due to its simplicity and because one of the div's is hidden, but unfortunately it copies both contents in one single line:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style> #HiddenURLdiv {display: none;}</style>
<div id="PreviewHeader">Hello stuff is here</div>
<div id="HiddenURLdiv">This one is hidden</div>
<a href="#" id="copystuff">Copy Stuff</a>
<div id="thecopiedtext"></div>
<script>
$("#copystuff").click(function() {
var temp = $("<input>");
$("body").append(temp);
var previewHeader = $("#PreviewHeader").text();
var HiddenURLdiv = $("#HiddenURLdiv").text();
var contentTogether = previewHeader + " " + HiddenURLdiv;
temp.val(contentTogether).select();
document.execCommand("copy");
$("#thecopiedtext").text(contentTogether);
temp.remove();
});
</script>
I've tried all the ways I know to break lines, wishing to get each div content on a separate line, but nothing seems to work.
I'm sure many of you know how to make this script to copy each div content on a separate line while keeping one of the div's hidden.
" + HiddenURLdiv; ? – Nathan Stockton Jun 07 '20 at 12:18
` as suggested but also: `$("#thecopiedtext").html(contentTogether);` – Roko C. Buljan Jun 07 '20 at 12:24
` won't work because you're adding it as text `$("#thecopiedtext").text(contentTogether);` change to `$("#thecopiedtext").html(contentTogether);` – Rainbow Jun 07 '20 at 12:24
` but than you need to use `.html()`, not `.text()` – Roko C. Buljan Jun 07 '20 at 12:25