I need to show a dynamic Bootstrap tooltip on a button that explains why the button is disabled (ie. "Fill out your name/address/phone", or some variation). I'm using Bootstrap/jQuery/KnockoutJS for the project.
I was having problems showing the Bootstrap tooltip on a disabled button and found through some Googling that this is due to the fact that the underlying button events are not being fired (since the button is disabled, the Bootstrap tooltip doesn't work. D'oh!).
I'm using KnockoutJS with click bindings to handle button events.
Issue: Does anyone know of a clean/succinct way to disable button click events when the button is enabled and contains a specific CSS class (ie. btn-disabled
) - so that the KnockoutJS click event is not called, but the tooltip still shows up?
Sample of non-working code:
Note that with the enable:false
data-bind attribute, the tooltip will not show up. I was trying to recreate the behavior with style/event handling, but not really disable the control:
HTML:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script src="http://knockoutjs.com/downloads/knockout-3.2.0.js"></script>
...
<button id="testButton"
class="btn btn-default"
data-bind="click: clickTest, enable:false"
data-toggle="tooltip"
data-placement="left"
data-original-title="Test tool tip">
Test Button
</button>
Script:
<script type="text/javascript">
function TestViewModel() {
var self = this;
self.clickTest = function () {
console.debug("Test button has been clicked");
};
}
var testViewModel = new TestViewModel();
$(document).ready(function () {
$('#testButton').tooltip();
console.debug("document ready");
ko.applyBindings(testViewModel);
});
</script>