I need to display a tooltip on a disabled button and remove it on an enabled button. Currently, it works in reverse.
What is the best way to invert this behaviour?
For Bootstrap 3
HTML
<button
class="btn btn-success"
disabled
data-hint="Enabled">
<div class="sp-btn-overlap" data-hint="Disabled"></div>
Submit button
</button>
CSS
button { position:relative; }
.sp-btn-overlap {
position:absolute;
top:0;
left:0;
height:100%;
width:100%;
background:none;
border:none;
z-index:1900;
}
JS
$('button .sp-btn-overlap')
.popover({
trigger: 'hover',
container: 'body',
placement: 'bottom',
content: function() {
return $(this).parent().prop('disabled')
? $(this).data('hint')
: $(this).parent().data('hint');
}
});
$('button .sp-btn-overlap')
.popover({
trigger: 'hover',
container: 'body',
placement: 'bottom',
content: function() {
return $(this).parent().prop('disabled')
? $(this).data('hint')
: $(this).parent().data('hint'); }
});
button {
position:relative; /* important! */
}
.sp-btn-overlap {
position:absolute;
top:0;
left:0;
height:100%;
width:100%;
background:none;
border:none;
z-index:1900;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<button disabled class="btn btn-default" data-hint="Enabled">
<div class="sp-btn-overlap" data-hint="Disabled"></div>
Disabled button
</button>
<button class="btn btn-default" data-hint="Enabled">
<div class="sp-btn-overlap" data-hint="Disabled"></div>
Enabled button
</button>
You can simply override Bootstrap's "pointer-events" style for disabled buttons via CSS e.g.
.btn[disabled] {
pointer-events: all !important;
}
Better still be explicit and disable specific buttons e.g.
#buttonId[disabled] {
pointer-events: all !important;
}
Simply add the disabled class to the button instead of the disabled attribute to make it visibly disabled instead.
<button class="btn disabled" rel="tooltip" data-title="Dieser Link führt zu Google">button disabled</button>
Note: this button only appears to be disabled, but it still triggers events, and you just have to be mindful of that.
Based on Bootstrap 4
Disabled elements Elements with the disabled attribute aren’t interactive, meaning users cannot focus, hover, or click them to trigger a tooltip (or popover). As a workaround, you’ll want to trigger the tooltip from a wrapper or , ideally made keyboard-focusable using tabindex="0", and override the pointer-events on the disabled element.
<span class="d-inline-block" tabindex="0" data-toggle="tooltip" title="Disabled tooltip">
<button class="btn btn-primary" style="pointer-events: none;" type="button" disabled>Disabled button</button>
</span>
All the details here: Bootstrap 4 doc
This is what myself and tekromancr came up with.
Example element:
<a href="http://www.google.com" id="btn" type="button" class="btn btn-disabled" data-placement="top" data-toggle="tooltip" data-title="I'm a tooltip">Press Me</a>
note: the tooltip attributes can be added to a separate div, in which the id of that div is to be used when calling .tooltip('destroy'); or .tooltip();
this enables the tooltip, put it in any javascript that is included in the html file. this line might not be necessary to add, however. (if the tooltip shows w/o this line then don't bother including it)
$("#element_id").tooltip();
destroys tooltip, see below for usage.
$("#element_id").tooltip('destroy');
prevents the button from being clickable. because the disabled attribute is not being used, this is necessary, otherwise the button would still be clickable even though it "looks" as if it is disabled.
$("#element_id").click(
function(evt){
if ($(this).hasClass("btn-disabled")) {
evt.preventDefault();
return false;
}
});
Using bootstrap, the classes btn and btn-disabled are available to you. Override these in your own .css file. you can add any colors or whatever you want the button to look like when disabled. Make sure you keep the cursor: default; you can also change what .btn.btn-success looks like.
.btn.btn-disabled{
cursor: default;
}
add the code below to whatever javascript is controlling the button becoming enabled.
$("#element_id").removeClass('btn-disabled');
$("#element_id").addClass('btn-success');
$('#element_id).tooltip('destroy');
tooltip should now only show when the button is disabled.
if you are using angularjs i also have a solution for that, if desired.
pointer-events: auto;
does not work on an <input type="text" />
.
I took a different approach. I do not disable the input field, but make it act as disabled via css and javascript.
Because the input field is not disabled, the tooltip is displayed properly. It was in my case way simpler than adding a wrapper in case the input field was disabled.
$(document).ready(function () {
$('.disabled[data-toggle="tooltip"]').tooltip();
$('.disabled').mousedown(function(event){
event.stopImmediatePropagation();
return false;
});
});
input[type=text].disabled{
cursor: default;
margin-top: 40px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.3/js/tether.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<input type="text" name="my_field" value="100" class="disabled" list="values_z1" data-toggle="tooltip" data-placement="top" title="this is 10*10">