admin管理员组文章数量:1410737
I only need this to work on Google Chrome so there is no requirement for multi-browser patible code.
This is the JS function I use to copy the text to clipboard.
// Copy text to clip-board JS
function copy(txt){
var cb = document.getElementById("cb");
cb.value = txt;
cb.style.display='block';
cb.select();
document.execCommand('copy');
cb.style.display='none';
}
var var1='download here: \n www.link-to-download';
Here is my HTML
<button class="buttonClass" onclick="copy(softwareinstall)">Install software</button>
<textarea id="cb" style="display: none"></textarea>
When I click the button, it will copy to clipboard, however when I paste the content into somewhere else for example notepad and outlook, the text is all on one line and the \n does nothing.
I would like for the string to be split onto 2 seperate lines.
Thank you in advanced.
I only need this to work on Google Chrome so there is no requirement for multi-browser patible code.
This is the JS function I use to copy the text to clipboard.
// Copy text to clip-board JS
function copy(txt){
var cb = document.getElementById("cb");
cb.value = txt;
cb.style.display='block';
cb.select();
document.execCommand('copy');
cb.style.display='none';
}
var var1='download here: \n www.link-to-download.';
Here is my HTML
<button class="buttonClass" onclick="copy(softwareinstall)">Install software</button>
<textarea id="cb" style="display: none"></textarea>
When I click the button, it will copy to clipboard, however when I paste the content into somewhere else for example notepad and outlook, the text is all on one line and the \n does nothing.
I would like for the string to be split onto 2 seperate lines.
Thank you in advanced.
Share Improve this question edited Sep 16, 2020 at 9:58 srptn asked Sep 16, 2020 at 8:57 srptnsrptn 811 silver badge10 bronze badges 4-
try using
\r\n
for linebreaks.\n
is the unix way. – Mark Baijens Commented Sep 16, 2020 at 9:52 - @MarkBaijens I have tried using \r\n and this has had no effect, I have \n working using a different method, so I don't think it's the \n which is causing it not to work. – srptn Commented Sep 16, 2020 at 10:39
-
Your code works fine when changing
var1
tosoftwareinstall
– Mark Baijens Commented Sep 16, 2020 at 11:01 - @MarkBaijens Apologies, i wrote that out wrong on here, the actual code is correct. Still cant get it working. – srptn Commented Sep 16, 2020 at 11:22
2 Answers
Reset to default 3If it is an option, you could easily preserve you text content by using navigator.clipboard.writeText()
instead of document.execCommand()
as that does not copy from the DOM. Something like:
const str = "Text \n on \n different lines";
navigator.clipboard.writeText(str).then(() =>
console.log("copied")
);
This is because the input element doesn't support line breaks, so your \n gets lost. Try to use a textarea instead:
// Copy text to clip-board JS
function copy(txt){
var cb = document.getElementById("cb");
cb.value = txt;
cb.style.display='block';
cb.select();
document.execCommand('copy');
cb.style.display='none';
}
var var1='first line of text \n second line of text';
<button onclick="copy(var1)">Copy Option 1</button>
<textarea id="cb" style="display: none"></textarea>
本文标签: javascriptHow can I get my Copy and Paste function to keep new line formattingStack Overflow
版权声明:本文标题:javascript - How can I get my Copy and Paste function to keep new line formatting? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744944351a2633681.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论