5

I'm unsure of why I'm getting this error, but for some reason jQuery's $ is not being recognised?

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

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

}); /* END WIDNOW LOAD */

NOTE: changing $ to jQuery fixes the issue (so I'm sure jQuery is referenced correctly, am using version 2.1.4), but I would like to continue using $ for semantics.

Jeroen
  • 60,696
  • 40
  • 206
  • 339
Ilja
  • 44,142
  • 92
  • 275
  • 498
  • 4
    Your parameter is a `$` – Ed Heal Jun 13 '15 at 09:58
  • 1
    Rename your parameter, problem solved. Happy life carries on. – TaoPR Jun 13 '15 at 10:02
  • 2
    `$` is the event. You could pass it to ready method but not load method. This would be for example to handle `$` in wordpress `jQuery(document).ready(function($){...});` – A. Wolff Jun 13 '15 at 10:20
  • This is a duplicate from https://stackoverflow.com/questions/12343714/typeerror-is-not-a-function-when-calling-jquery-function I believe – xarlymg89 Jul 24 '19 at 11:03
  • Possible duplicate of [TypeError: $ is not a function when calling jQuery function](https://stackoverflow.com/questions/12343714/typeerror-is-not-a-function-when-calling-jquery-function) – xarlymg89 Jul 24 '19 at 11:04

4 Answers4

9

You are overriding the $ variable inside your function, because you have an argument with the same name.

Remove the $ argument and $ will again refer to the global scoped one, 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 into load. I suggest the same as Anik Islam Abhi's answer: use another name for the argument. For example e or eventArgs.

Note that you (or others landing here) might actually be trying to use a pattern that makes sure jQuery is available as $ inside certain scope (e.g. because there may be a conflict with another library also declaring $ in global scope). If that's 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 your code inside a function which is executed immediately with jQuery passed in as an argument. Because $ is the name of the argument of that function, you'll know for sure that $ is equal to the global jQuery within that function's scope.

Community
  • 1
  • 1
Jeroen
  • 60,696
  • 40
  • 206
  • 339
  • $ could refers to something else than jQuery – A. Wolff Jun 13 '15 at 10:22
  • @A.Wolff Good point. Could be OP or others are looking for a pattern to *make sure* that `$` refers to `jQuery`, I've added a footnote / second part with some code to handle that case. – Jeroen Jun 13 '15 at 10:29
  • Kudos for pointing out the IIFE wrapper, which binds `jQuery` to `$` as the function begins to execute. – vhs Jun 01 '17 at 05:36
2

You are overriding event parameter with $

Try like this

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

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

}); /* END WIDNOW LOAD */
bugwheels94
  • 30,681
  • 3
  • 39
  • 60
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
2

Maybe you wanted something like that?

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

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

}); /* END WIDNOW LOAD */
Serg
  • 736
  • 1
  • 9
  • 20
1
$ = jQuery; 

can work too. Not the best way though.

null
  • 11
  • 4
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 22 '22 at 18:26