0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScriptを使って、テキストをコピーさせる

Posted at

ダミーの入れ物を用意して、それを選択した状態にして、コピーをコマンド操作でしてあげる必要がある。

Javascript
        $('#btn').on('click', function(){
            const text = 'sample';

            const dummyText =  document.createElement('textarea');
            const bodyTag = document.getElementsByTagName('body')[0];

            dummyText.text(text);
            bodyTag.appendChild(dummyText);

            dummyText.select();
            document.execCommand('copy');

            bodyTag.removeChild(dummyText);
            alert('コピーしました。');
        });

##参考にさせていただいたサイト
https://webllica.com/copy-text-to-clipboard/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?