Copy to Clipboard

Copy to Clipboard

HI,

I've came across a topic where @jakecigar have responded with an answer ( https://jsfiddle.net/jakecigar/dbzm58tn/11/). I'm currently using the code he gave but I can't seem to figure out having it to my liking.

Here's the code that I've changed so far.

  1. <html>

  2. <head>
  3. <script src="http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  4. </head>

  5. <script>
  6. $(document).on('click', '.copy', function(e) {
  7.   var serialized = $(this.form).serializeArray()
  8.   var text = serialized.map(function(field) {
  9.     return field.name+": "+field.value
  10.   }).join("\n")

  11.   $("#copy").show().val(text)[0].select();
  12.   document.execCommand("copy");
  13.   $("#copy").hide();
  14.   alert("Copied the form: " + text);

  15.   return false

  16. })
  17. </script>

  18. <form>
  19.   <label>Name: <input type="text" name="Name" value="" size="30"></label><br>
  20.   <label>Address: <input type="text" name="Address" value="" size="30"></label><br>
  21.   <label>Phone #: <input type="text" name="Best Phone #" value="" size="30"></label><br>
  22. ********************<br>
  23.   <label>Model: <input type="text" name="Model" value="" size="30"></label><br>
  24.   <label>SN: <input type="text" name="SN" value="" size="30"></label><br>
  25.   <button class=copy>copy</button>
  26. </form>
  27. <textarea id=copy name="hide" style="display:none;"></textarea>

  28. </html>
When clicking copy in the form created. It will paste it like this:


Name: Test
Address: Test
Best Phone #: Test
Model: Test
SN: Test

I need to have it pasted as: -


Name: Test
Address: Test
Best Phone #: Test
***************************
Model: Test
SN: Test

So it's like how the form looks like.



Hoping someone could help out.