I'm creating several types of jQuery UI Tooltips. One type:
- Open when you click an element (say a ? icon)
- Should allow the user to click inside the tip so that text can be selected/copied or interacted with.
- Should close when user clicks anywhere other than inside the tip.
I'm almost there, except for closing the tip. My project has a special clickoutisde event that can be bound to any element (see fiddle).
In the code below, it's bound to the element triggering the tooltip - in this case, the icon. So the tip closes if you click anywhere other than the icon.
My question is, can I somehow move this event from the icon to the tooltip itself (the .ui-icon class) so the tip closes if you click anywhere other than inside the tip?
Or do I need some other way? Thanks!
//
// EXAMPLE: Click to Open + Click Outside to Close
// Works on any element with the `.has-tip-pop` class + `title` attribute
// @requires ui-clickoutside.js
//
$( function() {
$('.has-tip-pop')
// Stop tip's default close.
.bind("mouseleave", function (event) {
event.stopImmediatePropagation();
})
// Initialize tip.
.tooltip({
// Start as `disabled` so we can `enable` with `click`.
disabled: true,
// A `ui-bg-` style provided by this theme.
tooltipClass: "ui-bg-black-black",
// Tip above h-centered
position: { my: "center bottom", at: "center top-10", },
})
// Open the tooltip.
.on('click', function () {
$(this).tooltip('enable').tooltip('open');
})
// Click outside ORIGINAL element to close.
// How to bind this to `.ui-tooltip`?
.bind( "clickoutside", function(){
$(this).tooltip('disable');
})
// Handle bug #10689 Memory Leak
.each(function(idx, element) {
var ele = $(element);
ele.tooltip({
"close": function(evt, ui) {
ele.data("ui-tooltip").liveRegion.children(":not(:last)").remove();
}
});
});
} ); // function
CLARIFICATIONS
I'm new to JS - I'm still learning the abstractions and semantics. Please be as explicit as possible with feedback :)
This type of tooltip is for just-in-time help - user clicks a ? icon or similar element and sees either:
- Text that they can select/copy - currently in element's
titleattribute. - Text + More Info link they can click - currently in tooltip.js content.
The memory leak is a known issue - it occurs on the default tooltip and my example includes the best working suggestion for dealing with it.
The clickoutside event is by Ben Alman - it's documented here and is visble in my fiddle.
SPECIFIC QUESTIONS
Is it conceptually wrong to use the Tooltip widget for the behaviors I describe above?
When I began this journey, I didn't think so - there are plenty of people and plugins doing similar things, I just couldn't find an example I could decipher or that got me all the way there. But if this isn't the best approach, what would you suggest instead?
I use the clickoutside event for closing dialogs and similar - I don't understand it, but it's easy to use and works. But is it the best way to approach closing tooltips when a user clicks anywhere but inside the tip itself?
If clickoutside is appropriate, and I understand it correctly, then it needs to be bound to the .ui-tooltip wrapper - the logic being, "if you click anywhere outside the tooltip, it will close".
It should look something like $('.ui-tooltip').bind(...), but for the current tooltip, and not all tooltips because we have another type that uses default mouse in/out behavior.
I can see in the code that handles the memory leak that you can get at the current tooltip's .ui-tooltip div, but don't know how to apply that here?