question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Text not copied to clipboard in disabled textarea

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

26reactions
zenorochacommented, Sep 30, 2015

Have you tried readonly instead of disabled?

12reactions
Damini25commented, Sep 24, 2018

solution- onCopy() { const copyText = (<HTMLInputElement>document.getElementById(‘1’)); copyText.disabled = false; copyText.select(); document.execCommand(‘Copy’); copyText.disabled = true; }

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow Copy/Paste in a disabled input text box in Firefox browsers
Firefox does not allow clipboard manipulation through JavaScript for valid security concerns. Any suggestion? Is there a work around this?
Read more >
[Solved] Text area disabled, copy text? - CSS-Tricks
Hello,. Is it possible to have a text area or similar, which users can copy code from to their clipboard? Can the text...
Read more >
253870 - Can't copy text from disabled textarea or text input.
There appears to be no way to select the text in these disabled fields and copy it to the clipboard. Reproducible: Always Steps...
Read more >
3 Ways to Disable Copy Text in Javascript & CSS - Code Boxx
This tutorial will walk through various ways on how to disable copy text with Javascript and CSS. Free example code download included.
Read more >
How to Disable Text Selection, Copy, Cut, Paste and Right-click
It's not a difficult task to make a text unselectable. All you need to do is to disable the text selectivity for all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found