Bootstrap tooltip getting it to work within an anchor tag

1k Views Asked by At

I have the following code, which show up fine:

 <a href="#" data-toggle="tooltip" data-placement="bottom" title="Message">
     <img src="~/Content/images/trackerBlack.png" />
 </a>

I need to get it to work with the following:

     <a href="#" class="dropdown-toggle" role="button" aria-expanded="false">

         <img src="~/Content/images/trackerBlack.png" /> 
         
        <span class="badge notificationeyebrow">
                0
         </span>

     </a>

Not sure how to get it to work within an anchor tag.

1

There are 1 best solutions below

0
On BEST ANSWER

The easiest way to achieve this would be to add a wrapper. The approach for where/how you would add the wrapper is mostly personal preference but the way I would do it is to change your dropdown-toggle from a hyperlink to a div, and move the hyperlink inside the newly-created wrapper:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-w1Q4orYjBQndcko6MimVbzY0tgp4pWB4lZ7lr30WKz0vr/aWKhXdBNmNb5D92v7s" crossorigin="anonymous"></script>

<div class="dropdown-toggle" role="button" aria-expanded="false">
  <a href="#" data-toggle="tooltip" title="Message" placement="top">
    <img src="https://placehold.it/25x25" /> 
    <span class="badge notificationeyebrow">0</span>
  </a>
</div>

<script>
  $(function () {
    $('[data-toggle="tooltip"]').tooltip()
  });
</script>

In this fashion you're going to retain all of the desired structure... your dropdown-toggle will still function, and the area that houses your tooltip will still behave like a hyperlink. You could handle this in other ways though... for example you could leave your hyperlink as you already have it, and create a <div> inside the hyperlink that houses your tooltip wrapper.