1

Why do I copy to clipboard not working, pls help me.

In my code, I want to copy message. This is function loading data by ajax.

var listCopy = [];

        listCopy = getSelectableList(_this);

        var arr_post = {

            relationshipId: listCopy,

        };

        uiCopy = '';

        var data_end = $.ajax({

            url: APP_BASE_URL + 'index.php/chat/getContentMessageCopy',

            type: 'POST',

            data: arr_post,

            async: false,

            beforeSend: function(argument) {

                // that.attr('disabled', 'true');

            },
            success: function(data) {

                var result = JSON.parse(data);

                uiCopy = '<div class="copy_message">';

                $.each(result, function( index, value ) {

                    uiCopy += '<div class="copy_message_content">' + value[0].CONTENT + '</div>';

                });          

                uiCopy += '<div class="copy_message_auth">' + result[0][0].SENDER_NAME + '</div>';

                uiCopy += '<div class="copy_message_time">' + result[0][0].SEND_DATE + '</div>';

                uiCopy += '</div>';

                uiCopy = '//copy:' + uiCopy;

                return data;

            }

        }).responseText;

        pushToClipboard(uiCopy);


function pushToClipboard(stringCopy) {

    var stringBufferCopy = stringCopy;

    var input = '<textarea id="temporary" style="position:absolute;opacity:0;"></textarea>';

    clipboardBuffer = '<textarea id="txtInputBuffer" style="position:absolute;opacity:0;" class="answer-text-area" maxlength="3000" spellcheck="false" >' + stringBufferCopy + '</textarea>';

    $("body").append(input);

    $("body").append(clipboardBuffer);

    $("#temporary").val($("#txtInputBuffer").text()).focus().select();

    document.execCommand("copy");

    $("#temporary").remove();

    $("#txtInputBuffer").remove();

    if (window.clipboardData) {

        window.clipboardData.setData('Text', stringCopy);

    }

}

Normal, function document.execCommand("copy") it working, but if loading data by ajax that not working.

Huy
  • 109
  • 2
  • 5

0 Answers0