Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - copy from sweetalert2 button confirm

how can I copy from my button "Copy URLs" when is clicked?

function cdwUrlsMDBE() {
            var prd = document.getElementById("prd");

            if (prd.checked == true) {
                Swal.fire({
                    type: 'success',
                    html: 'i am a text that should be copy',
                    width: 'auto',
                    confirmButtonText: 'Copy URLs',
                })

Thanks

like image 614
lud31991 Avatar asked Sep 12 '26 17:09

lud31991


1 Answers

You could do it creating a button via html and Bootstrap, in this way you could attach an handler on the button via onOpen.

Something like this

Swal.fire({
    title: "Copy it!",
    html: 
        '<textarea id="text_to_be_copied" class="swal2-input" readonly>Some text you want to copy</textarea>' +
            '<button type="button" class="btn btn-default" id="btn-copy" style="margin-left:5px">Copy</button>' +
            '<button type="button" class="btn btn-primary swal-confirm" id="btn-ok" style="float:right" disabled>Text have been copied!</button>' +
        '</div>',
    showConfirmButton: false,
    type: "success",
    onOpen: () => {
        $("#btn-copy").click(() => {
            $("#btn-ok").prop('disabled', false);

            $("#text_to_be_copied").select();
            document.execCommand("copy");
        });

        $("#btn-ok").click(() => {
            Swal.close();   
        });
    }
});

In this way the "confirmation button" is displayed only after you use the copy one, but you could decide to get rid of that just by putting Swal.close() inside the handler of the copy button. In this way after clicking che copy button, the popUp will close.

You could choose to use also an input tag instead of a textarea depends on your needs.

like image 161
InstictV Avatar answered Sep 15 '26 06:09

InstictV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!