-1

I am trying to make copy to clipboard. It's working on div element but if i use it in datatable that time not working

here is my code

->editColumn('value', function ($data) {
            return '<span>'.$data->value.'</span> <i class="fa fa-copy" title="Copy to Clipboard" data-toggle="tooltip" onclick="copyToClipboard(this,'.$data->value.')"></i>';
        })

  function copyToClipboard(em,text) {
            var dummy = document.createElement("textarea");
            dummy.style.display = 'none'
            document.body.appendChild(dummy);
            dummy.value = text;
            dummy.select();
            document.execCommand("copy");
            document.body.removeChild(dummy);
            $(em).attr('data-original-title', 'Copied')
                .tooltip('fixTitle')
                .tooltip('show');
        }

it shows Uncaught SyntaxError: missing ) after argument list . How can i fix ?

Kabir
  • 31
  • 1
  • 5

2 Answers2

1

Change to this to not have issues with content

->editColumn('value', function ($data) {     
  return '<span>'.$data->value.'</span> <i class="fa fa-copy" title="Copy to Clipboard" data-toggle="tooltip"></i>';
  })

using

$('.fa-copy').on('click', function() {
  copyToClipboard(this,$(this).prev().text());
})

you cannot have display none on the textarea if you want to create a copy

$('.fa-copy').on('click', function() {
  const text =  $(this).prev().text();
  console.log(text)
  copyToClipboard(this, $(this).prev().text());
})

function copyToClipboard(em, text) {
  var dummy = document.createElement("textarea");
 // dummy.style.display = 'none'; // cannot be display none
  document.body.appendChild(dummy);
  dummy.value = text;
  dummy.select();
  document.execCommand("copy");
//  document.body.removeChild(dummy);
//  $(em).attr('data-original-title', 'Copied')
//    .tooltip('fixTitle')
//    .tooltip('show');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.9.2/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/fontawesome.min.css" >
<span>Data value from span</span> <i class="fa fa-copy" title="Copy to Clipboard" data-toggle="tooltip"></i>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

The error message tells you what is wrong. There is a missing closing parenthesis here

->editColumn('value', function ($data) { return '<span>'.$data->value.'</span> <i class="fa fa-copy" title="Copy to Clipboard" data-toggle="tooltip" onclick="copyToClipboard(this,'.$data->value.')"></i>'; })

C. Peck
  • 3,641
  • 3
  • 19
  • 36