Why does JavaScript not work?

advertisements

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.


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