S
S
Smi_ed2019-03-03 16:09:13
JavaScript
Smi_ed, 2019-03-03 16:09:13

How to set up Clipboard for iOS?

There is a code that works except for iOS
//php

<div class="current-id clearfix" onclick="copyToClipboard('#current-id')">

//js
function copyToClipboard(element) {
    var $temp = $("<input>");
    $("body").append($temp);
    $temp.val($(element).text()).select();
    document.execCommand("copy");
    $temp.remove();
}

And there is a code that works under iOS
//js
window.Clipboard = (function(window, document, navigator) {
    var textArea,
        copy;

    function isOS() {
        return navigator.userAgent.match(/ipad|iphone/i);
    }

    function createTextArea(text) {
        textArea = document.createElement('textArea');
        textArea.value = text;
        document.body.appendChild(textArea);
    }

    function selectText() {
        var range,
            selection;

        if (isOS()) {
            range = document.createRange();
            range.selectNodeContents(textArea);
            selection = window.getSelection();
            selection.removeAllRanges();
            selection.addRange(range);
            textArea.setSelectionRange(0, 999999);
        } else {
            textArea.select();
        }
    }

    function copyToClipboard() {        
        document.execCommand('copy');
        document.body.removeChild(textArea);
    }

    copy = function(text) {
        createTextArea(text);
        selectText();
        copyToClipboard();
    };

    return {
        copy: copy
    };
})(window, document, navigator);

Clipboard.copy('text to be copied');

How to adapt it so that instead of the static 'text to be copied', the dynamic value '#current-id' is copied?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timokins, 2019-03-03
@timokins

The easiest:
or
replace with
and use
Clipboard.copy('#current-id');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question