Clipboard.js: Cannot copy from trigger attribute when in BS Modal

Created on 3 Sep 2019  路  3Comments  路  Source: zenorocha/clipboard.js

Minimal example

Example: https://jsfiddle.net/adzbyt3q/5/

Expected behaviour

Text should be copied from the trigger element's attribute (data-clipboard-text="text") even when the trigger is in a Bootstrap modal.

Actual behaviour

Text cannot be copied from the trigger element's attribute (data-clipboard-text="text") when that trigger element happens to be within a Bootstrap modal.

Browsers affected

Issue occurs in latest versions of Chrome, Firefox, and Internet Explorer.

Notes

I am rather new to Clipboard.JS, so I hope I am not overlooking something. The console records a successful copy (and no error), but nothing appears on the actual clipboard.

In the Fiddle, you'll see that copying from the trigger's own attribute works fine, until you place that trigger in a modal. Also in the Fiddle, this issue does not occur when the trigger is targeting a different element, even when that different element is also within the modal.

Most helpful comment

Set the container value to the modal's ID:

var clipboard = new ClipboardJS('.btn', {
    container: document.getElementById('exampleModal')
}); 

All 3 comments

I experienced the same bug, but your workaround saved me

Set the container value to the modal's ID:

var clipboard = new ClipboardJS('.btn', {
    container: document.getElementById('exampleModal')
}); 

Thanks @jamesbanogon!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

han8435762 picture han8435762  路  3Comments

XZZsr picture XZZsr  路  5Comments

168tickets picture 168tickets  路  6Comments

johnymontana picture johnymontana  路  7Comments

flydev-fr picture flydev-fr  路  3Comments