How do I access a variable from document ready function in jQuery function?
I have a click function to reset content to its' original state. I have set a variable with the original content in the document.ready function. I am trying to access that variable in a function within the jQuery.function. It fails. Is it possible to do this another way, if at all? If so, how?
The first alert works, the second fails, which proves that the variable is not defined within the jQuery.function block.
Since the content is dynamic, if I set the var in the jQuery function block rather than the document.ready block, won't it then retrieve the current state rather than the initial state?
Designbyonyx
The following two code snippets perform the same exact action.
// This is kind of the old way of doing it$(document).ready(function() {});// This is a shorthand for the above script$(function() {});// And this is another way people are doing it (including myself) // This way, you don't have to worry about conflicting with other scripts(function($) { $(function() { // All code in here fires on document ready });}(jQuery));
It is worth noting that the last method is good if you have multiple other script libraries, or if you are developing plugins or widgets that could be placed in other peoples sites.
Otherwise, you are best off using the shorthand method I described above.
Open in new window