Select and Copy text from a div with jQuery

时间:2019-05-31 11:41:22

标签: jquery

I've the following code:

<div class="modal-body">
    <div class="text-right fz-sm">
        <a href="#" data-action="copy-link">Copy link</a>
    </div>
    <pre>
        https//example.com
    </pre>
</div>

Using the link Copy link, I would like to select the link in <pre> block and copy it.

Here my try:

$('[data-action="copy-link"]').on('click', function(event) {
    event.preventDefault();

    $(this).closest('.modal-body').find('pre').select();
    document.execCommand('copy');
});

But it doesn't work.

Do you know why please ?

Thanks.

0 个答案:

没有答案