title: jQuery $.each variable Scope
body: <p>Having a scope issue problem with a <code>$.each</code> loop in <strong>jQuery</strong>.  How can I get a global variable in a function to set in a loop or at least pass something out of it?</p>
  
  <pre><code>var some_function = function() {
  
      // false by default
      var something = false;
  
      $.each(array, function(key, val) { 
         if (val == 'something')
         {
           // even if one item evaluates true I need to check outside of the loop
           something = true;
         }
      });
  
      if (something == true)
      {
         // do something else, but always false
      }
  }
  </code></pre>
  
  <p>Since I'm needing to evaluate all items in the array, and <strong>if only one is</strong> <code>true</code>, then do something additional, outside of the <code>$.each</code>. </p>
  
  <p><strong>Update</strong></p>
  
  <pre><code>$(document).ready(function () {
      something(); 
      $(':radio').trigger('change');
  )};
  </code></pre>
  
  <p>Ok, so this is the actual code.  It's alerting <strong>'false'</strong> at the bottom and then alerts <strong>'hello'</strong> twice, as if it's going in reverse order.</p>
  
  <pre><code>var something = function() {   
  
          var q_radios = {
              'radio1'        : 'radio1_selector', 
              'radio2'        : 'radio2_selector',  
          };
          var show_me = false;
  
          $.each(q_radios, function(name, q_selector) {  
              $('input:radio[name=' + q_selector + ']').change(function() {  
                      show_me = true; 
                      alert('hello'); 
              }); 
          });   
  
          if (show_me == true)
          {
              alert('yes');
          }
          else
          {
              alert('false');
          }
  
  };
  </code></pre>