Deka87 Deka87 - 28 days ago 23
Javascript Question

Clipboard.js not working in Bootstrap modal

I am trying to copy an input value with Clipboard.js: https://clipboardjs.com/. The input is located in a modal:

http://codepen.io/Deka87/pen/eBJOKY

new Clipboard('#copy', {
text: function(trigger) {
return $("#copy-input").val();
}
});


While it works outside of the modal, it fails to work when the input and the copy button are located in a modal window. I tried to init the clipboard function after the modal window is open:

$(".modal").on("shown.bs.modal", function() {
new Clipboard('#copy', {
text: function(trigger) {
return $("#copy-input").val();
}
});
});


However, it didn't solve the issue. Any ideas?

Answer

Try this fork: http://codepen.io/anon/pen/NbxWbQ I forgot to remove the console.log so just ignore that :)

<input type="text" class="form-control" id="copy-input" value="Copied successfully!"/>
    <br />
    <a href="#" id="copy" data-clipboard-target="#copy-input" class="btn btn-default">Copy input content to clipboard</a>

and

$(".modal").on("shown.bs.modal", function() {
  console.log('a', Clipboard, $('#copy'), $("#copy-input").val());
  var clipboard = new Clipboard('#copy')
});