-1

How can I access a function declared in $(document).ready event outside of its scope? In other words, how can I change a local function to global?

chembrad
  • 887
  • 3
  • 19
  • 33
Sravan0313
  • 83
  • 3
  • 10

2 Answers2

0

Why don't you declare the variable outside the scope and assign it inside the document ready method. Then you will be able to access it outside the document ready scope?

Mingo
  • 836
  • 3
  • 12
  • 19
0

You can add your function to window object in document ready handler:

$(document).ready(function(){
     window.YourFunc = function (){
     // Do something.
     };
});

And then call it whenever you want:

window.YourFunc();

But the best way is just define your function outside of ready handler.

Artem Vyshniakov
  • 16,355
  • 3
  • 43
  • 47