Check if a jquery tools tooltip is already assigned to a component
Asked Answered
W

1

3

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!

Wald answered 23/1, 2012 at 9:7 Comment(0)
R
5

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
});
Reputation answered 23/1, 2012 at 9:16 Comment(1)
Thanks, used a similar approach to verify that a tooltip has already been initialised so I can safely destroy it when needed.Constructive

© 2022 - 2024 — McMap. All rights reserved.