I followed the instructions on this answer to copy a URL when user clicks a button. Seems to work fine, except when I move my button into a modal window. I lose my click
event handler.
function copyTextToClipboard(text) {
[...] // working implementation
}
$(function() {
var currentPostUrl = window.location.href + "?ref=blogshare"
var currentPostTitle = $('.post-title').text().trim();
$('.js-copy-bob-btn').on('click', function(event) {
copyTextToClipboard(currentPostUrl);
console.log('copied')
});
$('#myModal').appendTo("body")
});
Here's my HTML:
<!-- Modal -->
<div id="myModal" class="modal fade" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" type="button" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div class="row modal-icons">
<div class="col-xs-5 text-center">
<a class="copy-url js-textareacopybtn" title="Step 2: Copy URL Button">
<h3>Copy Shareable Link</h3>
</a>
<-- THE BELOW BUTTON DOES NOT WORK AS DESIRED -->
<button class="js-copy-bob-btn">Copy Url</button>
</div>
</div>
</div>
</div>
</div>
</div>
<-- THE BELOW BUTTON WORKS AS DESIRED (NO MODAL)-->
<button class="js-copy-bob-btn">Copy Url</button>
Where am I going wrong? Why does the second button (outside of modal) work but not the first button?