0

I have one function some where in the code as i specify below

$(function () {
   function triggerSortable() {
             //Here is some some code
   }
});

Now inside

$(document).ready(function () { 
triggerSortable();
});

I have tried to call the function but is shows me below error.

Uncaught ReferenceError: triggerSortable is not defined
  • `triggerSortable` is defined inside of the anonymous function passed to the `document.ready()` method. As such, it's scope is that anonymous function, which means you can only call it from within that scope. Also, `$(function()...)` and `$(document).ready(function....` do the same thing. You can just combine them. – Scott Marcus Mar 24 '20 at 12:09
  • [This should help you understand the issue.](https://stackoverflow.com/questions/41385835/invocation-context-and-execution-context-in-javascript-are-we-talking-of-th/41386097#41386097) – Scott Marcus Mar 24 '20 at 12:11
  • 1
    Also take your time to learn *when* you actually need `$(function() { ... })`. Aside from restricting the visibility of functions, there is no reason to put function definitions there. – Felix Kling Mar 24 '20 at 12:19
  • @FelixKling YEs sir you are right. I have put the code in document.ready and it is working :) – Fresher Programmer Mar 24 '20 at 12:27

0 Answers0