1

I have a problem and the problem is greater because it's seen as a duplicate by some people here (of: How do I copy to the clipboard in JavaScript?).

I have

<div class="ce">
   <div class="lotB">9</div>
   <div class="lotB">3</div>
   <div class="lotB">28</div> == $0
  </div>

I want these inner text in form of 9 3 28, so I set up a string which equals 9 3 28

It's done like that:

var x = document.getElementsByClassName("lotB");
var count = 0;
var text = "";
for (var i = 0; i < x.length && count<3; i++) {
  var nums = x[i].textContent;
  text = text + nums.toString() + " ";
  count++;
}

Then text = "9 3 28"

Now my question, how can I directly copy the value of text to my clipboard such that I can post it somewhere else?

  • 2
    People are saying it's a duplicate because it is... you linked to the answer in your question, even. – Steve Archer Sep 18 '18 at 21:10
  • 1
    Yeah looks like a duplicate, https://stackoverflow.com/a/30810322/1309377 in there says the Async Clipboard API can copy text to the clipboard directly from a variable, which sounds exactly like what you want to do. – Andrew Lohr Sep 18 '18 at 21:12
  • Possible duplicate of [How do I copy to the clipboard in JavaScript?](https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript) – Calvin Nunes Sep 18 '18 at 21:17

1 Answers1

0

I have used this in my applications: https://www.npmjs.com/package/copy-to-clipboard

Ken Bigler
  • 587
  • 1
  • 4
  • 15