0

to sum it up quickly.. I have a jQuery tooltip on one of my table elements with a specific data attribute to select it. it works great, makes an ajax request, returns data and puts it in the tooltip. only problem is every time i mouse over the table element the ajax request gets triggered again.

    $('p').tooltip({ 
        items: "[data-whatever]",
        content: function(){
            var el = $(this),
                content = el.data('ajax-content');
            if(content)
                return content;    
            return 'waiting for ajax';
        },
        open: function(){
            var elem = $(this),
                id = elem.data('whatever');
            $.ajax('/echo/html/' + id).always(function(res)  {
                elem.tooltip('option', 'content', res[0].label);
                elem.data('ajax-content', res[0].label);
            });
        }
    });

all of this works fine ( i made a few changes to the code so its not identical to what i did because its incorporated into my works module pattern).. but is there a simple way to only hit the open function one time?

any help would be appreciated!

2 Answers 2

1

You can check if 'ajax-context' data is already set. If so do not trigger the ajax request.

$('p').tooltip({ 
    items: "[data-whatever]",
    content: function(){
        var el = $(this),
            content = el.data('ajax-content');
        if(content)
            return content;    
        return 'waiting for ajax';
    },
    open: function(){
        var elem = $(this),
            label = elem.data('ajax-content');
        if (label) {
          elem.tooltip('option', 'content', label);
        } else {
          var id = elem.data('whatever');
            $.ajax('/echo/html/' + id).always(function(res)  {
                elem.tooltip('option', 'content', res[0].label);
                elem.data('ajax-content', res[0].label);
            });
        }
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

I ended up going with your answer as its a better practice to check in this way. thanks again
1

Try this, store ajax response in element data, now check in open event if tooltip exists in data, if yes show it else cal ajax.

$('p').tooltip({ 
    items: "[data-whatever]",
    content: function(){
        var el = $(this),
            content = el.data('ajax-content');
        if(content)
            return content;    
        return 'waiting for ajax';
    },
    open: function(){
        if($(this).data("ajax-content") == undefined) {
            var elem = $(this),
                id = elem.data('whatever');
            $.ajax('/echo/html/' + id).always(function(res)  {
                elem.tooltip('option', 'content', res[0].label);
                elem.data('ajax-content', res[0].label);
            });
        }
        else {
            $(this).tooltip('option', $(this).data("ajax-content"));
        }
    }
});

5 Comments

you were really close... .data('tooltip') needs to be ajax-content... and then in the else, elem is undefined so it needs to be $(this)
Yes, it was not tested, i was delivering my idea only. :)
I know :) i was just telling you what needs to be fixed so I can accept the answer.. future ppl can see the correct answer
@JohnRuddell i have stored data in toolip, so i am fetching from toolip. pl check
ah i see now... i use data-ajax in the content, so either the tooltip needs to be changed or the data-ajax.. either way should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.