bootstrap-5twitter-bootstrap-tooltip

Using dropdown button together with tooltip causes a JavaScript error in BS 5


when using a dropdown button together with a tooltip throws a JavaScript error in Bootstrap 5. Please run this snippet:

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

    <div class="btn-group">
      <button id="btnFingerprints" type="button" class="btn btn-circle btn-secondary me-2 dropdown-toggle" data-bs-toggle-second="tooltip" data-bs-toggle="dropdown" aria-expanded="false" title="Testing">
          <i class="ti-exchange-vertical"></i>
      </button>

      <div class="dropdown-menu">
        <button class="dropdown-item" type="button" id="btnBajarHuellas">
            <i class="ti-download me-2"></i>Bajar huellas
        </button>
        
        <button class="dropdown-item" type="button" id="btnSubirHuellas">
            <i class="ti-upload me-2"></i>Subir huellas
        </button>
      </div>

      <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

<script>
let tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle-second="tooltip"]'))
    tooltipTriggerList.map(function (tooltipTriggerEl) {
        return new bootstrap.Tooltip(tooltipTriggerEl)
    })
</script>

Some points to consider here:

How can I do that?

Thanks

Jaime


Solution

  • Bootstrap doesn't allow more than one instance per element, so the solution is simple, just wrap the button inside a container that contains the tooltip:

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
      integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    
    <div class="btn-group">
      <div data-bs-toggle-second="tooltip" title="Testing">
        <button id="btnFingerprints" type="button" class="btn btn-circle btn-secondary me-2 dropdown-toggle"
          data-bs-toggle="dropdown" aria-expanded="false">
          <i class="ti-exchange-vertical"></i>
        </button>
    
        <div class="dropdown-menu">
          <button class="dropdown-item" type="button" id="btnBajarHuellas">
            <i class="ti-download me-2"></i>Bajar huellas
          </button>
    
          <button class="dropdown-item" type="button" id="btnSubirHuellas">
            <i class="ti-upload me-2"></i>Subir huellas
          </button>
        </div>
      </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
      integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
    
    <script>
      let tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle-second="tooltip"]'))
      tooltipTriggerList.map(function (tooltipTriggerEl) {
        return new bootstrap.Tooltip(tooltipTriggerEl)
      })
    </script>