Vinny
Vinny

Reputation: 789

How to use SweetAlert with clipboard.js library?

I'm trying to use SweetAlert with the clipboard.js library. I have tried simulating a click on a HTML button when the user confirm on SweetAlert, but it didn't work.

Here is what I've tried to do:

My HTML:

<button class="btn btn-primary" id="copyBtn" data-clipboard-target="#info_block" onclick="copyText();">
    Copy Text!
</button>

<div id="info_block" name="info_block">
    Some example text.
</div>

My JavaScript function:

var clipboard = new Clipboard('.btn');
clipboard.on('success', function(e) {
    console.log(e);
});
clipboard.on('error', function(e) {
    console.log(e);
});

And the structure of my SweetAlert:

swal({
  title: "Copy the text?",
  text: "Are you sure you want to copy it to clipboard?",
  type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Yes, copy it!",
  closeOnConfirm: false
},
function(){
  $("#copyBtn").click(); // simulates click on html button.
  swal("Copied!", "The text has been copied.", "success");
});

It works fine without sweet alert, but it won't work simulating a click on the copy button, and I can't figure out a way to get it working.

Upvotes: 2

Views: 4491

Answers (2)

Freitz
Freitz

Reputation: 198

With this variant, the newer SweetAlert2.js can be used.

function copyText(){

    var copy = $('.copy_text').val();
    $('.copy_text').select();
    document.execCommand('copy');
    
    Swal.fire({
        icon: 'success',
        title: 'Text copied to clipboard',
        text: copy,
        showConfirmButton: false,
        timer: 3000
    });
    
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>

<input type="text" class="copy_text" value="Hello World!">
<button onclick="copyText()">Copy to clipboard</button>

Upvotes: 1

Alexander Higgins
Alexander Higgins

Reputation: 6923

Your sweet alert was calling $("#copyBtn").click(); which I am not sure is necessary.

In any case, you have set onclick="copyText();" in this button but never defined a copyText function.

Either define the function or remove the onclick attribute.

Here's working code with an empty copyText function defined.

var clipboard = new Clipboard('.btn');
clipboard.on('success', function(e) {
    console.log('Copied text: ' + e.text);
});
clipboard.on('error', function(e) {
    console.log(e);
});
function copyText() {

}

swal({
  title: "Copy the text?",
  text: "Are you sure you want to copy it to clipboard?",
  //type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Yes, copy it!",
  closeOnConfirm: false
},
function(){
  $("#copyBtn").click(); // simulates click on html button.
  swal("Copied!", "The text has been copied.", "success");
}
);
/*fit into snippet window*/
.sweet-alert { margin: auto; transform: scale(.75); }
<link href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
<script src="https://cdn.jsdelivr.net/npm/clipboard@1/dist/clipboard.min.js"></script>


<button class="btn btn-primary" id="copyBtn" data-clipboard-target="#info_block" onclick="copyText();">
    Copy Text!
</button>

<div id="info_block" name="info_block">
    Some example text 
</div>

If you want to fire the alert on the button click then put the swal function inside of the function you defined in onclick:

Your sweet alert was calling $("#copyBtn").click(); which I am not sure is necessary.

In any case, you have set onclick="copyText();" in this button but never defined a copyText function.

Either define the function or remove the onclick attribute.

Here's working code with an empty copyText function defined.

var clipboard = new Clipboard('.btn');
clipboard.on('success', function(e) {
  console.log('Copied text: ' + e.text);
});
clipboard.on('error', function(e) {
  console.log(e);
});

function copyText() 
{
  swal({
      title: "Copy the text?",
      text: "Are you sure you want to copy it to clipboard?",
      //type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "Yes, copy it!",
      closeOnConfirm: false
    },
    function() {
      swal("Copied!", "The text has been copied.", "success");
    });
}
/*fit into snippet window*/

.sweet-alert {
  margin: auto;
  transform: scale(.75);
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
<script src="https://cdn.jsdelivr.net/npm/clipboard@1/dist/clipboard.min.js"></script>


<button class="btn btn-primary" id="copyBtn" data-clipboard-target="#info_block" onclick="copyText();">
    Copy Text!
</button>

<div id="info_block" name="info_block">
  Some example text
</div>

Upvotes: 0

Related Questions