G
G
geniusperson2018-03-10 01:11:03
JavaScript
geniusperson, 2018-03-10 01:11:03

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

1 answer(s)
0
0xD34F, 2018-03-10
@geniousperson

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);

UPD. Taken from the comments:
you need to copy to the plus, there may not always be 3 characters

You can find it '+'with indexOf.
Or cut out what is in front of it with regular expressions: . .match(/[^+]*/)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question