Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-11-16
7
Medium Priority
?
464 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:Justin Mathews
Justin Mathews 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Technology Partners: 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
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

618 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