jqueryjquery-toolsjquery-tooltip

Check if a jquery tools tooltip is already assigned to a component


I have a component to which I assign a tooltip upon first mouseenter (sort of a lazy assignment of tooltip to component)

I use the lazy approach since there are many tooltip'able components and I don't want to pre-assign the tooltips to all of them.

$(document).delegate(".tooltipable", "mouseenter", function () {
    $(this).tooltip(... options ...);
    $(this).tooltip().show(); // The tooltip will not appear on first `mouseenter` so I have to explicitly show it here
});

This works just fine. I would like to improve it so the tooltip will not be created on every mouseenter by checking if the tooltip was already created for this component.

How can that be done?

Thanks in Advance!


Solution

  • You can try something like this.

    $(document).delegate(".tooltipable", "mouseenter", function () {
        var $this = $(this);
        if(!$this.data("tooltipset")){
           $(this).tooltip(... options ...)
           .data("tooltipset", true);
        }
        $(this).tooltip().show(); // The tooltip will not appear on first `mouseenter` so I have to explicitly show it here
    });