Bootstrap 4 Tooltip Continues to Show, Appears Unsightly

Can you please execute the following HTML test case in order to help identify a solution for making a Bootstrap 4 tooltip disappear when a button is disabled while the tooltip is hovering over the button? The specific issues I am facing are outlined within the code.

The Stack Overflow editor is rejecting my attempt to include the details as comments within the code, so I will reiterate some key points here. When a button is disabled and the cursor hovers over it, the tooltip remains visible, which is aesthetically unpleasing. Any assistance with resolving this would be greatly appreciated.

Thank you

<!DOCTYPE html>
    <html lang="en">
    <head>
    <title>Bootstrap 4 Tooltip Issue</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    </head>
    <body>
    <div class="container">
    <h3>Bootstrap 4 Tooltip Issue</h3>
    <br> "Other button disabled" scenario works correctly.
    <br> Initial hover over buttons reveals tooltips.
    <br> Upon clicking Button A to disable Button B:
    <br> - Button B is disabled with no tooltip.
    <br> - Tooltip on Button A functions properly.
    <br> <br>
    <button type="button" id="button_a" class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" title="Disables Button B" onclick='change_button ("button_b", true);'>
    Button A
    </button>
    <button type="button" id="button_b" class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" title="Disabled by Button A">
    Button B
    </button>
    <br> <br> <hr>
    <br> "Same button disabled" scenario fails.
    <br> Hover over Button C displays tooltip.
    <br> Clicking Button C to disable itself results in:
    <br> - Button C gets disabled.
    <br> - However, the tooltip on Button C persists, causing a visual issue.
    <br> <br>
    <button type="button" id="button_c" class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" title="Disables Button C" onclick='change_button ("button_c", true);'>
    Button C
    </button>
    <button type="button" id="button_d" class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" title="Enables Button C" onclick='change_button ("button_c", false);'>
    Button D
    </button>
    <br> <br> <hr> <br>
    When a button is disabled while its tooltip is shown, the tooltip persists.
    <strong>
    Could you provide guidance on how to hide the tooltip when Button C is disabled?
    </strong>
    Ideally, the tooltip should disappear upon Button C being disabled,
    consistent with the default behavior of Bootstrap with disabled buttons.
    As the disabled button may later become enabled again, deleting the title attribute is not desired.
    Essentially, I want Bootstrap to handle Button C the same way it handles Button B when disabled.
    Despite attempting various solutions, I have been unable to resolve this issue.
    There are cases where a button performs an action before becoming disabled,
    hence any assistance is greatly valued. Thank you
    </div>
    <script>
    $(document).ready(function(){$('[data-toggle="tooltip"]').tooltip();});
    function change_button (button_id, bool_val) {
    var button_obj = document.getElementById (button_id);
    button_obj.disabled = bool_val;
    }
    </script>
    </body>
    </html>

Answer №1

To integrate functionality into your change_button() method, include the logic to hide, disable and enable the tooltips.

$(document).ready(function() {
  $('[data-toggle="tooltip"]').tooltip();
});

function change_button(button_id, bool_val) {
  var button_obj = document.getElementById(button_id);
  button_obj.disabled = bool_val;

  // Control tooltip behavior
  if (bool_val) {
    $('#' + button_id).tooltip('hide').tooltip('disable');
  } else {
    $('#' + button_id).tooltip('enable');
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />

<div class="container">
  <br> The "Other button disabled" scenario functions correctly.
  <br> Begin by hovering over buttons and observing tooltips during hover.
  <br> Then, clicking Button A will disable Button B as intended:
  <br> - Button B is disabled without a tooltip visible.
  <br> - Tooltip remains functional for Button A.
  <br> <br>
  <button type="button" id="button_a" class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" title="Disables Button B" onclick='change_button ("button_b", true);'>
    Button A
    </button>
  <button type="button" id="button_b" class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" title="Disabled by Button A">
    Button B
    </button>
  <br> <br>
  <hr>
  <br> The "Same button disabled" scenario encounters difficulties.
  <br> Start by hovering over Button C and observing the tooltip.
  <br> Next, disabling Button C yields an undesirable outcome:
  <br> - Button C successfully disables, but its persistent tooltip appears unattractive.
  <br> <br>
  <button type="button" id="button_c" class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" title="Disables Button C" onclick='change_button ("button_c", true);'>
    Button C
    </button>
  <button type="button" id="button_d" class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" title="Enables Button C" onclick='change_button ("button_c", false);'>
    Button D
    </button>
</div>

Answer №2

Make sure to close the tooltip once the button has been disabled.

  function toggleButton(buttonId, value) {
    var buttonElement = document.getElementById(buttonId);
    $("#" + buttonId).tooltip("hide");
    buttonElement.disabled = value;
  }

Answer №3

As the original poster, I was taken aback by azeos' assertion that there were differences in behavior between the two answers, as my tests in IE 11 and Chrome 74 showed no discrepancies. To further investigate, I compared the two answers side by side. Both versions alter the change_button function from the original post:

function change_button(button_id, bool_val) { /* A */
  var button_obj = document.getElementById(button_id);
  $("#" + button_id).tooltip("hide");
  button_obj.disabled = bool_val;
  }

//////

function change_button(button_id, bool_val) { /* B */
  var button_obj = document.getElementById(button_id);
  button_obj.disabled = bool_val;
  if (bool_val) {
    $('#' + button_id).tooltip('hide').tooltip('disable');
    }
  else {
    $('#' + button_id).tooltip('enable');
    }
  }


To clarify, the expected behavior (in line with other bootstrap tooltips) is:

  • Page loads
  • Button C displays tooltip on hover
  • Click Button C
  • Button C becomes disabled and its tooltip disappears
  • Click Button D
  • Button C is re-enabled and tooltip appears on hover again


My additional testing revealed the following results:

  • IE 11 (Windows): both versions A and B are functional
  • Chrome 74 (Windows): both versions A and B are functional
  • Opera 60 (Windows): both versions A and B are functional
  • Safari 10 (Mac): both versions A and B are functional
  • Firefox 67 (Windows): version A fails, while B works.


Contrary to azeos' speculation, in Firefox 67, the tooltip for Button C did not disappear as anticipated.

I plan to conduct more extensive testing when time permits. Additionally, I have raised this issue on GitHub under twbs/bootstrap to address it as a potential bug or concern, and will monitor any feedback received.

For Safari testing, I utilized the Lambdatest website, while other tests were conducted directly on a Windows 7 desktop.

If you have any other comments or suggestions, please feel free to share them, especially regarding scenarios where version B may fail or any insights on smartphone testing.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Bootstrap navigation menu is functioning properly when opening, but has an issue when attempting

Creating a mirrored version of an HTML file using NuxtJS, I've included the following code snippet in the Navbar component for my NuxtJS project. <template> <section id="navigation-menu" class="menu menu3 cid-sLhoPz7Ycg" ...

Implementing a jQuery AJAX form that handles file uploads

I am having trouble finding a solution for this issue, so I need some assistance... Form: <div class="status alert alert-success" style="display: none"></div> <form id="main-contact-form" class="contact-form" name="contact-form" method="po ...

Despite successfully connecting to my webservice, the ajax function encounters an error

<script type='text/javascript'> $(document).ready(function () { $("#submit").click(function (e) { e.preventDefault(); var userName = $("#username").val(); var pwd = $("#password").val(); authenticate(use ...

Display a jQuery loading window

Need help with displaying a loading div when making an ajax post request. I've tried the following code but it's not working. Can someone please assist in resolving this issue? $(document).ready(function() { $('a.pagerlink').click( ...

Interactive HTML Slides Creator

I am considering developing an online "Slides viewer/Editor" using HTML5 slides with Html5 and Jquery. I want to ensure that this is feasible and will be compatible on mobile devices. While there are many Slides Viewers available, none of them have editi ...

organize divs in a nested angular style

Looking to showcase div elements in a specific layout within my Angular project: https://i.sstatic.net/RH0lF.png The current HTML structure is as follows: <div class="outer-wrapper"> <div class="image" ng-repeat="image in images" ng-if="sho ...

Leverage JavaScript or CSS to create a paper button without the ink effect

Currently, I am utilizing JavaScript to dynamically create paper-buttons in the following manner: function createButtons(dieDefaults) { for(var i = 0; i < dieDefaults.length; i++) { var btn = document.createElement("paper-button"); var txt = d ...

Is there a way to prevent the second ul from appearing?

Seeking assistance with navigation. When clicking on the "Athletics" tab in the provided link, the second ul displays. Is there a method to hide this second ul and have it only appear upon hovering? ...

I require the variable to be accessible from all corners

Is there a way to access the variable "finalPrice" in a different function? I'm trying to achieve this and could use some guidance. function available(id){ $.ajax({ method:"GET", }).done(function(data){ for( ...

Angular 2 - Bootstrap - Toggle Hide/Show

Is there a way to create a unique ID for each collapse item? The issue I'm facing is that when I click anywhere in the list, the first item always collapses. I've tried giving the div the data.id, but it doesn't seem to work. <div class ...

Ways to break apart a string of text without a regular pattern

Here is the data I am working with: Huawei Y7P Art-L28 (4/64gb) (AAAAAAAAAAAAAA) EXP:02/19/2020 Huawei Y9 prime 2019 (BBBBBBBBBBBBBBB) EXP:07/17/2019 Oppo A31 4gb/128gb (CCCCCCCCCCCCCCC) Vivo Y15 5000mah 4GB/64GB (1901) (DDDDDDDDDDDDDDD) EXP:06/14/2019 I ...

Deactivating jQuery triggers a malfunction in the update panel

I am facing a challenge with my jQuery dialog that contains update panels and utilizes jQuery validate. In my attempts to disable the validation using jQuery unbind function in beforeClose, I encountered issues where the update panels would crash. Is the ...

Having trouble getting a jQuery variable to work as an argument in an onclick function for an HTML

success: function(jsonresponse) { data = JSON.stringify(jsonresponse); $.each(JSON.parse(data), function (index, item) { var Id=item.id; var JobId=item.JobId; var eachrow = "<tr>" + "<td>" + item.id + "</td>" ...

Understanding the Contrast between Relative Paths and Absolute Paths in JavaScript

Seeking clarification on a key topic, Based on my understanding, two distinct paths exist: relative and absolute. Stricly relative: <img src="kitten.png"/> Fully absolute: <img src="http://www.foo.com/images/kitten.png"> What sets apart R ...

Identify the CSS Framework being used in conjunction with Selenium

I have developed a program that crawls through various web pages and conducts tests using Selenium. My current task is to identify which CSS Frameworks are utilized on these websites for statistical analysis. Currently, I am using the FireFox Webdriver to ...

Using the <li dir="..."> tag can cause list indicators to break

Is there a method to utilize dir tags for <li> elements without disrupting the list indicators? Logically, they should all align on the same side and RTL <li> elements in an otherwise LTR document should either be left-aligned if it's only ...

Transferring data through Ajax, Jquery, and PHP: A seamless process

My goal is to develop an attendance button that adds +1 to the total number of people attending when clicked by the user. I am looking for a way to achieve this without having to refresh the page. Ideally, I would like to connect it to a database to upda ...

Does Webkit reserve the class name ".ad-space" for a specific purpose?

Can you believe this? I'm working with 960.gs and for some reason my ads class labeled ".ad-space" is vanishing in Webkit browsers. It functions properly in Firefox, but not in Chrome or Safari. Could it be a bug in a Webkit addon, like Adblock Plus? ...

Is it possible for comments in HTML and CSS to cause issues with rendering?

Could HTML or CSS comments potentially lead to rendering issues? HTML Comment Example: <!-- additional details --> CSS Example: /* more information */ ...

Verify if the expression can be found in the DIV element's style

Recently, I encountered a situation where a DIV contains a background image that is either set directly or generated as an SVG. I needed to figure out how to determine when the DIV contains the SVG-string. Here are my two DIVs: <div class="cover m ...