How to update variable value using click and switch
<pre><code>$(document).ready(function() {

    var varChange = '';
    var baseVar = 50;

    selectBaseVar = function(){
        switch (varChange) {case 101: varChange = 501; break;
                            case 501: varChange = 101; break;
                            default:  varChange = 101; break;}
        console.log('internal val: '+varChange);
        return varChange;
    };

    $("#myButton").click(function(){
        selectBaseVar();
    });

    selectBaseVar();

    console.log('external val: '+varChange);
    console.log('base baseVar = '+baseVar);
    baseVar += varChange;    // How do I update this value upon the button click event?
    console.log('updated baseVar = '+baseVar); // it works only in the document.ready state
});
</code></pre>

<p>Works for the <code>document.ready</code> state but not for the <code>click</code> event. A scope problem is maybe the cause.</p>

<p>I don't know how to workaround this issue</p>

<p><br/>The intention is to left baseVar as constant @ 50
<br/>Increase the value of baseVar, due 
<br/>baseVar += varChange;
<br/>Switch varChange, and update baseVar</p>

<p><br/>In docuent.ready state we got:</p>

<p><br/>internal val: 101
<br/>external val: 101
<br/>base baseVar = 50
<br/>updated baseVar = 151</p>

<p><br/>if I click, respectively I got internal values as expected:
<br/>internal val: 501
<br/>internal val: 101</p>

<p><br/>But (updated baseVar = 151)  no matter what value it’s switching </p>

<p><br/>And not what I’m up to, that is affecting the external variable <strong>baseVar</strong> with the values of:
<br/>updated baseVar = 151
<br/>updated baseVar = 551</p>

<p><br/>note: there is a good reason to leave the <strong>baseVar</strong> variable out of the scope of the <strong>selectBaseVar()</strong> for the sake of other functions that benefit from the switch result of the same, but been out of the scope of <strong>selectBaseVar()</strong>.</p>

<p><br/>Thanks</p>
