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?
You can wrap the disabled button and put the tooltip on the wrapper:
<div class="tooltip-wrapper" data-title="Dieser Link führt zu Google">
<button class="btn btn-default" disabled>button disabled</button>
</div>
If the wrapper has display:inline
then the tooltip doesn't seem to work. Using display:block
and display:inline-block
seem to work fine. It also appears to work fine with a floated wrapper.
UPDATE Here's an updated JSFiddle that works with the latest Bootstrap (3.3.6). Thanks to @JohnLehmann for suggesting pointer-events: none;
for the disabled button.
http://jsfiddle.net/cSSUA/209/
If it helps anyone, I was able to get a disabled button to show a tooltip by simply putting a span inside it and applying the tooltip stuff there, angularjs around it...
<button ng-click="$ctrl.onClickDoThis()"
ng-disabled="!$ctrl.selectedStuff.length">
<span tooltip-enable="!$ctrl.selectedStuff.length"
tooltip-append-to-body="true"
uib-tooltip="Select at least one thing to enable button.">
My Butt
</span>
</button>
If you're desperate (like i was) for tooltips on checkboxes, textboxes and the like, then here is my hackey workaround:
$('input:disabled, button:disabled').after(function (e) {
d = $("<div>");
i = $(this);
d.css({
height: i.outerHeight(),
width: i.outerWidth(),
position: "absolute",
})
d.css(i.offset());
d.attr("title", i.attr("title"));
d.tooltip();
return d;
});
Working examples: http://jsfiddle.net/WB6bM/11/
For what its worth, I believe tooltips on disabled form elements is very important to the UX. If you're preventing somebody from doing something, you should tell them why.
You can imitate the effect using CSS3.
Simply take the disabled state off the button and the tooltip doesn't appear anymore.. this is great for validation as the code requires less logic.
I wrote this pen to illustrate.
CSS3 Disabled Tooltips
[disabled] {
&[disabled-tooltip] {
cursor:not-allowed;
position: relative;
&:hover {
&:before {
content:'';
border:5px solid transparent;
border-top:5px solid black;
position: absolute;
left: 50%;
transform: translate(-50%, calc(-100% + -5px));
}
&:after {
content: attr(disabled-tooltip);
position: absolute;
left: 50%;
transform: translate(-50%, calc(-100% + -15px));
width:280px;
background-color:black;
color:white;
border-radius:5px;
padding:8px 12px;
white-space:normal;
line-height:1;
}
}
}
}
<button type="button" class="btn btn-primary" disabled-tooltip="I am a disabled tooltip using CSS3.. You can not click this button." disabled>Primary Button</button>
These workarounds are ugly. I've debugged the problem is that bootstrap automatically set CSS property pointer-events: none on disabled elements.
This magic property causes that JS is not able to handle any event on elements that matches CSS selector.
If you overwrite this property to default one, everything works like a charm, including tooltips!
.disabled {
pointer-events: all !important;
}
However you shouldn't use so general selector, because you will probably have to manually stop JavaScript event propagation way you know (e.preventDefault()).
Here is some working code: http://jsfiddle.net/mihaifm/W7XNU/200/
$('body').tooltip({
selector: '[rel="tooltip"]'
});
$(".btn").click(function(e) {
if (! $(this).hasClass("disabled"))
{
$(".disabled").removeClass("disabled").attr("rel", null);
$(this).addClass("disabled").attr("rel", "tooltip");
}
});
The idea is to add the tooltip to a parent element with the selector
option, and then add/remove the rel
attribute when enabling/disabling the button.