Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: n is not a function

I'm building a Wordpress site that has the following bit of JS:

jQuery(window).load(function($) {
    console.log('Everything loaded');
    $('.slides').fadeIn();
});

I'm seeing "Everything loaded" in the console, but the following line causes an error:

Uncaught TypeError: n is not a function

I don't understand what's causing the problem. The JS file has jQuery as a dependency, and there are other jQuery functions that are working fine. It's just the above section that's causing an error.


Here's a screenshot from the console, because some people are having difficulty believing the above code is causing the error.

enter image description here

enter image description here

like image 626
Chuck Le Butt Avatar asked Mar 14 '23 04:03

Chuck Le Butt


1 Answers

The issue is because you have set the event parameter as provided to the handler function with the name of $. This overwrites the jQuery instance of $, hence the error. You just need to remove the $ from the function parameters:

jQuery(window).load(function() { // < remove the $ here
    console.log('Everything loaded');
    jQuery('.slides').fadeIn();
});

Note that from your comments you're looking to alias the jQuery variable as a $ after using noConflict(). To do this you can use this signature document.ready handler:

jQuery(function($) {
    $(window).load(function() {
        console.log('Everything loaded');
        $('.slides').fadeIn();
    });
});
like image 122
Rory McCrossan Avatar answered Mar 23 '23 18:03

Rory McCrossan