themis93 themis93 - 4 days ago 5
Javascript Question

jQuery - Function not defined

I have two .js files. On the first .js file i declare and execute a function like this:

(function($){
function something(){
//code here
}

$(document).ready(function(){
something();
});
})(jQuery);


Also, I want to execute the something() function in the second .js. My code is the following:

(function($){
$(document).ready(function(){
//more code
something();
//more code
});
})(jQuery);


However, when the program is executed, I get the following error:
Uncaught ReferenceError: something is not defined(…)

Possible Solution:
I solved the problem declaring the function as:

(function($){
this.something = function(){
//code here
}

})(jQuery);


Is this 100% right?

Answer

It's normal, you declared your function something in an IIFE, so you can't using it outside your block. Try to declare your function outside (function($) ...)() block.

Comments