?
Solved

How do I access a variable from document ready function in jQuery function?

Posted on 2010-11-16
7
Medium Priority
?
462 Views
Last Modified: 2012-06-22
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.  
$(document).ready(function() {
	var savedContent = document.getElementById('ajaxContainer').innerHTML;
});

jQuery(function($) {
		
		///////////////////////////////////////////////// for resetting email content /////////////////////////////////////////////////
		
		$("#resetContent").click(function() {
			alert('within resetContent click function');
			alert('savedContent:\n' + savedContent);
			$('#ajaxContainer').innerHTML = savedContent;
		});
		
});

Open in new window

0
Comment
Question by:brianmfalls
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 16

Assisted Solution

by:jmatix
jmatix earned 800 total points
ID: 34148414
Try this:
$(document).ready(function() {
	$.savedContent = document.getElementById('ajaxContainer').innerHTML;
});

jQuery(function($) {
		
		///////////////////////////////////////////////// for resetting email content /////////////////////////////////////////////////
		
		$("#resetContent").click(function() {
			alert('within resetContent click function');
			alert('savedContent:\n' + $.savedContent);
			$('#ajaxContainer').innerHTML = $.savedContent;
		});
		
});

Open in new window

0
 
LVL 14

Expert Comment

by:Designbyonyx
ID: 34148463
declare the variable OUTSIDE of any function calls:
var savedContent;
$(document).ready(function() {
        savedContent = document.getElementById('ajaxContainer').innerHTML;
});

jQuery(function($) {
                
                ///////////////////////////////////////////////// for resetting email content /////////////////////////////////////////////////
                
                $("#resetContent").click(function() {
                        alert('within resetContent click function');
                        alert('savedContent:\n' + savedContent);
                        $('#ajaxContainer').innerHTML = savedContent;
                });
                
});

Open in new window

0
 
LVL 14

Accepted Solution

by:
Designbyonyx earned 1200 total points
ID: 34148481
And, you're code is a little redundant.  This would achieve the same purpose:
jQuery(function($) {
                var savedContent = document.getElementById('ajaxContainer').innerHTML;
                ///////////////////////////////////////////////// for resetting email content /////////////////////////////////////////////////
                
                $("#resetContent").click(function() {
                        alert('within resetContent click function');
                        alert('savedContent:\n' + savedContent);
                        $('#ajaxContainer').innerHTML = savedContent;
                });
                
});

Open in new window

0
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 
LVL 1

Author Comment

by:brianmfalls
ID: 34148542
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?
0
 
LVL 14

Expert Comment

by:Designbyonyx
ID: 34148657
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));

Open in new window

0
 
LVL 1

Author Comment

by:brianmfalls
ID: 34148678
Okay.  Thanks.  :)
0
 
LVL 14

Expert Comment

by:Designbyonyx
ID: 34148711
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question