title: How to save var value outside ajax success function?
body: <p>I am trying to make some form validation functions. Here is what I have:</p>
  
  <pre><code>&lt;script&gt;
  $(document).ready(function() {
  
  var myObj = {}; 
  
  $('#username').keyup(function () {
  
      id = $(this).attr('id');
      validateUsername(id);       
  
  });
  
  function validateUsername(id){
  
  var username = $("#"+id).val();
  
      $.ajax({
          url : "validate.php",
          dataType: 'json',
          data: 'action=usr_id&amp;id=' + username,
          type: "POST",
          success: function(data) {
  
              if (data.ok == true) {
  
                  $(myObj).data("username","ok");
  
              } else {
  
  
                  $(myObj).data("username","no");
  
              }
  
          }
      }); 
  
  
  } // end validateusername function
  
  
  $('#submit').click(function(){
  
      if (myObj.username == "ok")  {
  
          alert("Username OK");
  
      } else {
  
          alert("Username BAD");
  
      }   
  
  
  });
  
  
  
  
  }); // end doc ready
  </code></pre>
  
  <p>So you can see, when a key is pressed in the textbox, it checks if it's valid. The "data.ok" comes back correctly. The problem is based on the response, I define $(myObj).username. For some reason, I can't get this value to work outside the validateusername function. When clicking the submit button, it has no idea what the value of $(myObj).username is.</p>
  
  <p>I need to use something like this, because with multiple form fields on the page to validate, I can do something like:</p>
  
  <pre><code>if (myObj.username &amp;&amp; myObj.password &amp;&amp; myObj.email == "ok")
  </code></pre>
  
  <p>... to check all my form fields before submitting the form.</p>
  
  <p>I know I must just be missing something basic.... any thoughts?</p>
  
  <p>EDIT: SOLVED</p>
  
  <p>All I had to do was change <strong>var myObj = {};</strong> to <strong>myObj = {};</strong> and it's working like a charm. I think I've been staring at this screen waaaaay too long!</p>