0

If I have e.g. 20 lines of text, e.g.

<p class="textlist">Cheese</p>
<p class="textlist">Water</p>
<p class="textlist">Earth</p>
<p class="textlist">Helicopter</p>
<p class="textlist">Rabbit</p>

I wondered if anyone might be able to advise please, how I might be able to have a button on the page which a user can click, which copies the text values to the clipboard, so that all the user gets in their clipboard are the p innerHTML values in a list split by line breaks, e.g.

Cheese
Water
Earth
Helicopter
Rabbit

The example above is just a simple example, rather than the finished product I'l be working with, just something to use as an example to demonstrate the point.

I was thinking it might be possible using Javascript / jQuery / accessing the DOM, but I don't know how one would do it.

Amit Kumar
  • 5,888
  • 11
  • 47
  • 85

1 Answers1

0

Use .map in Jquery to collect all P tag data and append the text to some other element

 var data = $("p").map(function () {
    return "<div>" + $(this).text() + "</div>";
}).get();

$(".getDiv").append(data);

DEMO

Balachandran
  • 9,567
  • 1
  • 16
  • 26