I am trying to develop a "copy" button to copy numbers from a multi input form to the clipboard. I installed Clipboard.js and, point the id to the <form>
element so, everything works fine except that the numbers of each input are copied with a break line between them. Is there any way to remove any space or break line in between?
Should I add .replace(/[^0-9]/g, "")
somewhere?
new ClipboardJS('.btn_copy', {
});
.btn_copy {
background: rgba(50, 50, 90, 1);
min-height: 30px;
width: 50px;
padding: 15px 50px;
color: #e1e1e1;
font-family: sans-serif;
font-size: 1.2em;
display: inline-block;
margin-left: 50px;
cursor: pointer;
}
.btn_copy:hover {
background: rgba(50, 50, 90, 0.9);
}
.circle input {
border-radius: 999px;
float: left;
max-width: 100px;
font-size: 2em;
text-align: center;
height: 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.11/clipboard.js"></script>
<form id="tkt_1">
<div class="circle">
<input type="number" placeholder="00" />
</div>
<div class="circle">
<input type="number" placeholder="00" />
</div>
<div class="circle">
<input type="number" placeholder="00" />
</div>
</form>
<div class="btn_copy" data-clipboard-action="copy" data-clipboard-target="#tkt_1">Copy</div>
2
Answers
To remove any space or line break from a string you can use the
replace
method with'n'
for the line break and' '
for the space.Keep in mind that the statement
copy(formatedText)
is just symbolic. Use anything you want or need to copy the value to the clipboard.You don’t need a special library to put things into the clipboard. Just use
navigator.clipboard.writeText()
Here’s your same code but without needing clipboard.js or even jQuery.