SRAMPI
SRAMPI

Reputation: 373

jQuery plugin Tooltipster giving error for ajax call to load conent

I have the code like below and the error is from instance.content() line. I am loading all the content dynamically and executing below for all href tags. The error is Uncaught TypeError: instance.content is not a function How can I fix this

ex.find("a[href]").each(function (idx, el) {
var el = $(el);
var url = el.prop("href").substring(4);
el.attr("href", "");
el.addClass("popuplink");

el.tooltipster({
    content: 'Loading...',
    // 'instance' is basically the tooltip. More details in the "Object-oriented Tooltipster" section.
    functionBefore: function (instance, helper) {

        var $origin = $(helper.origin);

        // we set a variable so the data is only loaded once via Ajax, not every time the tooltip opens
        if ($origin.data('loaded') !== true) {

            $.get(url, function (data) {

            // call the 'content' method to update the content of our tooltip with the returned data
            instance.content(data);

            // to remember that the data has been loaded
             $origin.data('loaded', true);
            });
        }
    }

});

});

Upvotes: 1

Views: 753

Answers (2)

user2662680
user2662680

Reputation: 706

If anybody else is having this issue with V3 and wants this to work for V3 and not V4, you can see the old documentation here:

https://web.archive.org/web/20140404021936/http://iamceege.github.io/tooltipster#advanced

Basically they really changed how the functions worked between version so it is confusing. A working V3 example of this could be:

$('.tooltip').tooltipster({
    content: 'Loading...',
    functionBefore: function(origin, continueTooltip) {

        // we'll make this function asynchronous and allow the tooltip to go ahead and show the loading notification while fetching our data
        continueTooltip();
        
        // next, we want to check if our data has already been cached
        if (origin.data('ajax') !== 'cached') {
            $.ajax({
                type: 'POST',
                url: 'example.php',
                success: function(data) {
                    // update our tooltip content with our returned data and cache it
                    origin.tooltipster('content', data).data('ajax', 'cached');
                }
            });
        }
    }
});

Upvotes: 0

Louis Ameline
Louis Ameline

Reputation: 2889

Make sure you are using the files of Tooltipster v4. I see no other reason why it wouldn't work with the code you gave us.

Upvotes: 1

Related Questions