Answer the question
In order to leave comments, you need to log in
How to copy text to a specific location?
How can I copy text up to a certain part?
For example, I only need to copy 100!
<div class="p" id='p1'>100 + 600</div>
<button onClick="copyToClipboard('p1')"><img src='img/calc.png'></button>
function copyToClipboard(elementId) {
var aux = document.createElement("input");
aux.setAttribute("value", document.getElementById(elementId).innerHTML);
document.body.appendChild(aux);
aux.select();
document.execCommand("copy");
document.body.removeChild(aux);
}
Answer the question
In order to leave comments, you need to log in
Cut off as much as you need from the full text, such as .
Or instead do.slice(0, 3)
aux.focus();
aux.setSelectionRange(0, 3);
you need to copy to the plus, there may not always be 3 characters
'+'
with indexOf
. .match(/[^+]*/)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question