Stephen Watkins
Stephen Watkins

Reputation: 25775

Why does this JavaScript not work?

I'm using the qTip 1.0.0-rc3 plugin for jQuery. And, although it's not a big deal, I am curious as to why this works:

$(document).ready(function() {
    if (jQuery().qtip) {
        $('[data-qtip]').each(function() {
            var qTipContent = $(this).attr("data-qtip");
            $(this).qtip({ content: qTipContent });
        });
    }
});

and this does not work:

$(document).ready(function() {
    addToolTips();
});

function addToolTips() {
    if (jQuery().qtip) {
        $('[data-qtip]').each(function() {
            var qTipContent = $(this).attr("data-qtip");
            $(this).qtip({ content: qTipContent });
        });
    }
};

The former is being invoked within a function and the latter is not. Here is the error message from Firebug:

f(this).data("qtip") is null

I'm sure it's something stupid, but what am I missing?

Thanks.

Upvotes: 1

Views: 192

Answers (3)

Stephen Watkins
Stephen Watkins

Reputation: 25775

Wow, I feel stupid. I just found there was a hidden conflict in another one of my local files. I knew everything looked right! Thanks for the help.

Upvotes: 1

AndreKR
AndreKR

Reputation: 33697

The code you have given, is it executed in the "global scope" or is it wrapped by { and } (in another function or something)?

Upvotes: 1

bogatyrjov
bogatyrjov

Reputation: 5378

Try passing this to the addToolTips() as a parameter.

Upvotes: 0

Related Questions