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?
Asked
Active
Viewed 123 times
-1
-
1That's what scope means: the places where your function is known. So you can't. – Asciiom Aug 29 '12 at 14:17
-
this answer might help http://stackoverflow.com/questions/11811980/jquery-how-to-convert-local-variable-to-a-global-variable – Barlas Apaydin Aug 29 '12 at 14:31
2 Answers
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