The dollar sign ("$") is not a function

I'm not sure why I am getting this error, but for some reason jQuery is $

not recognized?

jQuery(window).load(function ($) {
    'use strict';

    /* Preloader */
    $(".status").fadeOut();
    $(".preloader").delay(1000).fadeOut("slow");

}); /* END WIDNOW LOAD */

      

NOTE: changing $

to jQuery

fixes the problem (so I'm pretty sure jQuery is linking correctly, I'm using version 2.1.4), but I'd like to continue using it $

for semantics.

+3


source to share


3 answers


You are overriding a variable $

inside your function because you have an argument with the same name.

Remove the argument $

and $

it will again refer to the global scope equal to jQuery

.

jQuery(window).load(function () {
    'use strict';

    /* Preloader */
    $(".status").fadeOut();
    $(".preloader").delay(1000).fadeOut("slow");

}); /* END WIDNOW LOAD */

      

You can use a parameter for the handler function passed to load

. I suggest the same as Anik Islam Abhi's answer : use a different name for the argument. For example e

or eventArgs

.



Note that you (or other people landing here) could actually use a template that ensures that it jQuery

is available both $

within a specific scope (for example, because there might be a conflict with another library also declaring $

globally). If this is the case, I suggest something along these lines:

(function($) {
    $(window).load(function () {
        'use strict';

        /* Preloader */
        $(".status").fadeOut();
        $(".preloader").delay(1000).fadeOut("slow");

    }); /* END WIDNOW LOAD */
}(jQuery));

      

This will wrap all of your code inside a function that is immediately executed with the help jQuery

passed as an argument. Since $

is is the argument name of this function, you know exactly what $

is global jQuery

within this function.

+7


source


You are overriding the event parameter with $

try it



jQuery(window).load(function (e) {
    'use strict';

    /* Preloader */
    $(".status").fadeOut();
    $(".preloader").delay(1000).fadeOut("slow");

}); /* END WIDNOW LOAD */

      

+2


source


Maybe you wanted something similar?

jQuery(document).ready(function ($) {
    'use strict';

    /* Preloader */
    $(".status").fadeOut();
    $(".preloader").delay(1000).fadeOut("slow");

}); /* END WIDNOW LOAD */

      

+1


source







All Articles