Copying to clipboard

You can select all the text in a textarea, and also fill it with the variable you want before. The textarea need to be visible for the text to be selected, and you can't juste hide it just after.

  1. //////////////////////////////////////
  2. // Javascript
  3. $(document).ready(function()
  4. {
  5. $('.copy-ids').on('click', function(e)
  6. {
  7. var to_copy = '';
  8.  
  9. $('.id_to_copy').each(function(index)
  10. {
  11. to_copy = to_copy + $(this).html() + ' ; ';
  12. });
  13.  
  14. $('.paste-ids').html(to_copy).css('visibility', 'visible');
  15. $('.paste-ids')[0].select();
  16. document.execCommand("copy");
  17.  
  18. // TODO : indicateur visuel
  19. });
  20. });
  21.  
  22. //////////////////////////////////////
  23. // HTML
  24. <input class="copy-ids" type="button" value="Copier les ids"/>
  25. <textarea class="paste-ids"></textarea>