Copy to clipboard



  • Hi guys,
    I am trying to implement a copy to clipboard button which should take the value of a textarea .
    I tried both the clipboard.js library (https://clipboardjs.com/)

    var copiedtext = new Clipboard("#btn-copy");
    

    and the plain javascript approach

    $("#btn-copy").on('click', function(event) {
            var copyTextarea = $("textarea").select();
            document.execCommand('copy');
        });
    

    but none is working on mobile.
    Any suggestions or ideas?


  • Onsen UI

    @chriscarex I looked into your question and I’m afraid on a hybrid mobile app your only option is to use a Cordova plugin such as this one. Both clipboard.js and the execCommand() function work only on browsers.



  • @misterjunio amazing thanks I will give it a try!



  • cordova plugin add https://github.com/VersoSolutions/CordovaClipboard.git

    module.controller('ClipboardCtrl', function($scope, $cordovaClipboard) {
    
      $cordovaClipboard
        .copy('text to copy')
        .then(function () {
          // success
        }, function () {
          // error
        });
    
      $cordovaClipboard
        .paste()
        .then(function (result) {
          // success, use result
        }, function () {
          // error
        });
    
    });
    
    

Log in to reply