title: Unable to pass variables to onreadystatechange
body: <p>This is the current source code:</p>
  <pre><code>var xhttp = new XMLHttpRequest();
    
  function passVars(var1, var2, var3) {
    if (var1.readyState == 4) {
      if (var1.status == 200) {
        var data = var1.responseText;
  
        if (data) {
          playSuccess();
          classSwitch(data, var2, var3);
        } else {
          playFailure();
          alert(&quot;Error: returned status code &quot; + var1.status + &quot; &quot; + var1.statusText);
        }
      }
    }
  }
    
  xhttp.onreadystatechange = function() { 
    passVars(xhttp, &quot;tab&quot;, &quot;p1234&quot;);
  };
  
  xhttp.open(&quot;POST&quot;, &quot;index.php&quot;, true);
  xhttp.send(formData); //formdata is a POST send to PHP's backend which returns responseText = 0 or 1 for var data
  
  function classSwitch(state, sOrigin, stateButtonID) {
    if (sOrigin === &quot;tab&quot;) {
      Monitored = state;
      if (state === &quot;1&quot;) {
        if (document.getElementById(&quot;setmonitoring&quot;).classList.contains(&quot;productmonitoringdisabled&quot;)) {
          document.getElementById(&quot;setmonitoring&quot;).classList.remove(&quot;productmonitoringdisabled&quot;);
        }
        document.getElementById(&quot;setmonitoring&quot;).classList.add(&quot;productmonitoringenabled&quot;);
      }
      
      if (state === &quot;0&quot;)  {
        if (document.getElementById(&quot;setmonitoring&quot;).classList.contains(&quot;productmonitoringenabled&quot;)) {
          document.getElementById(&quot;setmonitoring&quot;).classList.remove(&quot;productmonitoringenabled&quot;);
        }
        document.getElementById(&quot;setmonitoring&quot;).classList.add(&quot;productmonitoringdisabled&quot;);
      }
    }
    
    if (sOrigin === &quot;issues&quot;) {
      if (state === &quot;1&quot;) {
        if (document.getElementById(stateButtonID).classList.contains(&quot;productmonitoringdisabled&quot;)) {
          document.getElementById(stateButtonID).classList.remove(&quot;productmonitoringdisabled&quot;);
        } else document.getElementById(stateButtonID).classList.add(&quot;productmonitoringenabled&quot;);
      }
      
      if (state === &quot;0&quot;)  {
        if (document.getElementById(stateButtonID).classList.contains(&quot;productmonitoringenabled&quot;)) {
          document.getElementById(stateButtonID).classList.remove(&quot;productmonitoringenabled&quot;);
        } else document.getElementById(stateButtonID).classList.add(&quot;productmonitoringdisabled&quot;);
      }
    }
  }
  </code></pre>
  <p>Tried a lot of ways to pass them using mainly SO anwsers and each time <code>var2</code> and <code>var2</code> are <code>undefined</code>. This is used in an inventory control system, early alpha version. The idea is to pass element's id to change button class when the backend returns product's current monitoring state</p>
  <p>Any ideas how to pass those variables? Thanks in advance</p>