Clipboard.js: Text not copied to clipboard in disabled textarea

Created on 30 Sep 2015  Â·  7Comments  Â·  Source: zenorocha/clipboard.js

I am using clipboard.js to copy text from a disabled textarea (the text is programmatically generated and I don't want the user to edit it before copying):

<button class="copyBtn" data-clipboard-target="#myTextArea">Copy</button>
<textarea id="myTextArea" rows="10" disabled>My text here</textarea>

With the disabled property the text is not copied to the clipboard, despite the clipboard success event indicating that it is:

var clipboard = new Clipboard('.copyBtn');

        clipboard.on('success', function(e) {
            console.info('Action:', e.action);
            console.info('Text:', e.text);
            console.info('Trigger:', e.trigger);

            e.clearSelection();
        });

        clipboard.on('error', function(e) {
            console.error('Action:', e.action);
            console.error('Trigger:', e.trigger);
        });
Action: copy 
Text: My text here
Trigger: <button data-clipboard-target="#myTextArea" class="copyBtn">​

Removing the disabled property on the textarea, clipboard functions as expected and text is copied.

Most helpful comment

Have you tried readonly instead of disabled?

All 7 comments

Have you tried readonly instead of disabled?

Works fine with readonly, which makes more sense for my use-case. Thanks.

;D

I encountered this problem too.
Clipboard.js works with disabled on Chrome, but not on Firefox.
readonly is OK on both.

I think you should update the readme and the web site to mention about readonly, as I think many people may encounter this problem too, and they may give up this wonderful Clipboard.js because they think it's broken.

@ngocdaothanh is correct, disableddoesn't works on Firefox and yet it emits the success event.

solution-
onCopy() {
const copyText = (document.getElementById('1'));
copyText.disabled = false;
copyText.select();
document.execCommand('Copy');
copyText.disabled = true;
}

@Damini25 your solution worked for me. Thanks a lot!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

michael-letcher picture michael-letcher  Â·  3Comments

bodymindarts picture bodymindarts  Â·  5Comments

flydev-fr picture flydev-fr  Â·  3Comments

godoffrags picture godoffrags  Â·  4Comments

wpp picture wpp  Â·  7Comments