stevenbenner / jquery-powertip

:speech_balloon: A jQuery plugin that creates hover tooltips.
https://stevenbenner.github.io/jquery-powertip/
MIT License
820 stars 137 forks source link

How to know if powertip has already been initialized? #149

Closed vincentwansink closed 7 years ago

vincentwansink commented 8 years ago

After reloading part of the page with ajax I need to reset the title to the data collection for the powertip. However, sometimes I'm loading a brand new page using ajax into an existing page, in which case I need to initialize the powertip. So sometimes initialize, sometimes data collection.

I'd like to be able to write a generic function that can determine whether or not a powertip needs to be initialized or whether the data collection needs to be reset. How can I tell if a powertip has already been initialized?

stevenbenner commented 8 years ago

PowerTip automatically initializes itself when you run the powerTip() function. So you shouldn't have to do anything to detect if it has done all of its setup work.

If you're just trying to see if PowerTip is hooked to a specific element already, then there isn't a built in way to do that through PowerTip itself at the moment. You could check to see if the element has any of the jQuery data attributes that PowerTip uses (powertip, powertipjq, or powertiptarget). Something like this:

function elHasPowerTip($el) {
    var attrs = [ 'powertip', 'powertipjq', 'powertiptarget' ],
        hasTooltip = false;

    $.each(attrs, function(idx, attr) {
        if ($el.data(attr)) {
            hasTooltip = true;
        }
    });

    return hasTooltip;
}

What exactly is the problem you're encountering? If it's that tooltips are not displaying in this scenario it's probably because you destroyed all of the old elements (with events and tooltip data) when you rebuilt the page. You can fix this by running powerTip() on all of the newly created element that need tooltips.

If the problem you're seeing is that the tooltip content isn't changing then perhaps you're reusing the original elements, but didn't tell PowerTip about the change. See Changing the tooltip content in the next version docs.

vincentwansink commented 8 years ago

Here's my scenario. I have a container page with powertips, and then within that page I load sub page (within a div) and that page also has powertips. When I load that sub page, I initialize the powertips on that page, but in doing so it breaks the powertip that I had already initialized on the container page.

So basically, when the sub page loads, I need to initialize only some of the powertips that exist in the browser. I guess I could use different class names to differentiate, but I thought it might be cleaner if I could initialize all powertips, except those that are already initialized.

Perhaps the example code you posted will work for my purposes. I'll give it a shot.

stevenbenner commented 7 years ago

Haven't heard back if that worked or not, so I'm going to close this issue. If it hasn't and you think this is something that needs to be changed/added in PowerTip then let me know.