Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate variables inside the jQuery $(document).ready() base scope?
    text
    copied!<p>I'm trying to find a way to minimize the number of Selector look-ups. My issue is that I have a variable defined with base $(document).ready() that needs to be updated inside functions nested inside $(document).ready()</p> <p>Consider this example (EDIT: I updated it to be more explanatory)</p> <pre><code>&lt;script&gt; //var $current_page = $home_page; **&lt;--I DONT want to do this, going global and of course it doesn't work since $home_page isn't defined yet.** $(document).ready(function() { var $home_page = $("#home-page"); var $portfolio_page = $("#portfolio-page"); var $about_page = $("#about-page"); var $current_page = $home_page; // **&lt;--This variable, in this scope level, // is what I want updated** $("#home-btn").click(function () { $current_page.stop() $current_page.show() $current_page.animate({ duration: 360, easing: 'easeInCirc', complete: function() { $(this).css({ top: -700 }); } ); $current_page = $home_page; }); $("#portfolio-btn").click(function () { $current_page.stop() $current_page.show() $current_page.animate({ duration: 360, easing: 'easeInCirc', complete: function() { $(this).css({ top: -700 }); } ); $current_page = $portfolio_page; //&lt;--This needs to somehow update the // variable in the $(document).ready // scope, but not global** }); }); &lt;script&gt; </code></pre> <p>How can I update the variable $current_page <b>without</b> making it a global variable?</p> <p>EDIT: This is done to animate out the current page div when you click on a menu item. Yes, it's missing things, yes it may not make sense. It's just an example, not the actual application.</p> <p>I understand this example is still trivial for performance, just ignore that fact. I just want to know how to do achieve this, not a lesson on whether it's the best practice or performance. Thanks guys.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload