0

I have a site with the Bootstrap 5 theme. I created a button to copy a url.

It works, no problem.

  1. I want that when I click on the button, the tooltip displays "Lien copié". Currently we must redo a flyover to see the change.

  2. I want that when I click on the button and don't hover over it, it displays the default tooltip.

TEST :

// btn-clipboard.js
var clipboardShare = new ClipboardJS('.btn-clipboard-share');

clipboardShare.on('success', function(e) {
  document.getElementById('btn-clipboard-share').setAttribute('data-bs-original-title', 'Lien copié');
});

clipboardShare.on('error', function(e) {
  document.getElementById('btn-clipboard-share').setAttribute('data-bs-original-title', 'Erreur');
});

// tooltip.js
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
  return new bootstrap.Tooltip(tooltipTriggerEl)
})
<!doctype html>
<html lang="fr" class="h-100">

  <head>
    <meta charset="utf-8">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
  </head>

  <body>

    <button role="button" id="btn-clipboard-share" class="btn btn-outline-dark btn-clipboard-share m-3" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Copier le lien" data-clipboard-text="https://www.example.fr">https://www.example.fr</button>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js"></script>

  </body>

</html>
1234ABC
  • 3
  • 2

1 Answers1

0

You can change the tooltip while you’re hovering over the button by using Bootstap’s tooltip update() function followed by the show() function. Then you can attach a listener to listen for the mouse to leave. When the mouse leaves, you can switch the tooltip back.

One note: Bootstrap’s documentation for update says the function “Updates the position of an element’s tooltip.” It doesn’t say it will update the text, but it does. According to this Change Twitter Bootstrap Tooltip content on click, there used to be a function fixTitle that would update the text, but that’s no longer available (the function is available through _fixTitle).

Update

For your code version in Pastebin, there’s a complication with using tooltipList[ ] – you would need to use [0] for one button and [1] for the other. Since you’re creating separate ClipboardJS instances, it’s probably easiest to create separate tooltip instances with unique names, rather than having to track which element is [0] and which is [1].

I’ve updated my answer to support two buttons using separate elements and instances for each. In your Pastebin code, you show you’re going to be copying the content from a container (a modal), but the modals are currently not in your example.

I also enclosed everything inside a self-invoking expression to avoid any naming conflicts and encapsulate everything.

Update 2

For changing the tooltip text for modals, the tooltip needs to be specifically hidden when the mouse leaves. I'm not sure why a modal is different from a button, but since the tooltip was shown using a method, it seems to want a method to hide it.

Adding the line: tooltipShare.hide(); and tooltipDonation.hide(); to the callback functions after the mouse leaves will hide the tooltip.

Update 3

To switch from Copier le lien to Copier l'adresse with an apostrophe, change from single quote marks for defining strings to double quote marks.

tooltipShareButton.setAttribute('data-original-title', 'Copier le lien');

tooltipShareButton.setAttribute("data-original-title", "Copier l'adresse");

Because Copier l'adresse is so much longer than Lien copié, it would make sense to make some adjustments to the styling. If you can put the below styles in your head (or add to one of the CSS files) somewhere so they won’t get overwritten, they’ll help the tooltip look better.

<style>
  .tooltip-inner {
     width: 7rem;
  }

  .tooltip.show {
     opacity: 1;
  }
</style>

(function (doc, clip, boot) {
    // btn-clipboard.js
    var clipboardShare = new clip('#btn-clipboard-share');
    var clipboardDonation = new clip('#btn-clipboard-donation');
    var tooltipShareButton = doc.getElementById('btn-clipboard-share');
    var tooltipDonationButton = doc.getElementById('btn-clipboard-donation');
    var tooltipShare = new boot.Tooltip(tooltipShareButton);
    var tooltipDonation = new boot.Tooltip(tooltipDonationButton);

    clipboardShare.on('success', function(e) {
        function restoreTitle(e) {
            tooltipShare.hide();
            tooltipShareButton.setAttribute('data-bs-original-title', 'Copier le lien');
            tooltipShareButton.removeEventListener('mouseleave', restoreTitle);
        }
        tooltipShareButton.setAttribute('data-bs-original-title', 'Lien copié');
        tooltipShare.update();
        tooltipShare.show();
        tooltipShareButton.addEventListener('mouseleave', restoreTitle);
    });

    clipboardShare.on('error', function(e) {
        function restoreTitle(e) {
            tooltipShare.hide();
            tooltipShareButton.setAttribute('data-bs-original-title', 'Copier le lien');
            tooltipShareButton.removeEventListener('mouseleave', restoreTitle);
        }

        tooltipShareButton.setAttribute('data-bs-original-title', 'Erreur');
        tooltipShare.update();
        tooltipShare.show();
        tooltipShareButton.addEventListener('mouseleave', restoreTitle);
    });

    clipboardDonation.on('success', function(e) {
        function restoreTitle(e) {
            tooltipDonation.hide();
            tooltipDonationButton.setAttribute('data-bs-original-title', 'Copier le lien');
            tooltipDonationButton.removeEventListener('mouseleave', restoreTitle);
        }

        tooltipDonationButton.setAttribute('data-bs-original-title', 'Adresse copiée');
        tooltipDonation.update();
        tooltipDonation.show();
        tooltipDonationButton.addEventListener('mouseleave', restoreTitle);
    });

    clipboardDonation.on('error', function(e) {
        function restoreTitle(e) {
            tooltipShare.hide();
            tooltipDonationButton.setAttribute('data-bs-original-title', 'Copier le lien');
            tooltipDonationButton.removeEventListener('mouseleave', restoreTitle);
        }

        tooltipDonationButton.setAttribute('data-bs-original-title', 'Erreur');
        tooltipShare.update();
        tooltipShare.show();
        tooltipDonationButton.addEventListener('mouseleave', restoreTitle);
    });
}(document, ClipboardJS, bootstrap));
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js"></script>

<style>
    #btn-clipboard-share, #btn-clipboard-donation {
        width: 6rem;
    }
</style>

<button role="button" id="btn-clipboard-share" class="btn btn-outline-dark m-3" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Copier le lien" data-clipboard-text="https://www.example.fr/Share">Share</button>

<button role="button" id="btn-clipboard-donation" class="btn btn-outline-dark m-3" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Copier le lien" data-clipboard-text="https://www.example.fr/Donation">Donation</button>

When used in a modal, the initial tooltip doesn't go away (hide and dispose wouldn't make it go away) — but so long as the updated tooltip content is the same length or longer, it will cover it up, and both tooltips are removed when the mouse leaves.

Rich DeBourke
  • 2,873
  • 2
  • 15
  • 17
  • Thanks, I tested your answer and it works. I have to apply it to 2 buttons in 2 modal window. Is this code correct https://pastebin.com/P2Wt1ush ? – 1234ABC Apr 25 '21 at 11:38
  • I’ve updated my answer for your Pastebin – short answer is the code won’t work as it’s using the same [0] element. I think doing separate instances rather than using an array would be easier to maintain. I’d be curious to see how it runs with your modals. – Rich DeBourke Apr 25 '21 at 13:17
  • The behavior is weird. If I click on the button, the tooltip is displayed, but if I don't hover the button, it doesn't disappear. – 1234ABC Apr 25 '21 at 17:02
  • It seems that mouseleave isn't enough to close the tooltip, but adding a specific instruction to hide the tooltip seems to work. I've updated the code from your [page](https://pastebin.com/tgXxmXwL). – Rich DeBourke Apr 25 '21 at 22:34
  • Thank you it's perfect. I have one last question, in this line `tooltipDonationButton.setAttribute('data-original-title', 'Copier le lien');` I want to replace `Copier le lien` with `Copier l'adresse` but there is a problem with the `'` character. – 1234ABC Apr 26 '21 at 11:13
  • I have 2 website with 2 version of Bootstrap. The code works without problem with Bootstrap 5. But with Bootstrap 4 I have an error in the console with Tooltip (it makes the display of the page buggy) https://pastebin.com/vk1QaJVS and https://pastebin.com/z55NKqRj and https://ibb.co/fM0v7gV My Bootstrap 4 site is on Drupal 8. I am not very familiar with JS and I have certainly done something wrong with the initialization of Tooltip – 1234ABC Apr 26 '21 at 11:43
  • I've updated my answer to handle the apostrophe in the string (switch to double quote marks). For your Bootstrap 4 website, it would be best to start a new question so you're not mixing 4 and 5. – Rich DeBourke Apr 26 '21 at 14:30
  • Ok thank you I asked a new question herehttps://stackoverflow.com/questions/67273184/how-to-reset-bootstrap-4-tooltips-on-drupal-8 – 1234ABC Apr 26 '21 at 20:11