title: Is there a better way to include jquery event handlers with xmlhttprequest()
body: <p>I've written the real-time form validation necessary for my Site. It's included in my <code>javascript/jquery.js</code> file and does things like:</p>
  
  <pre><code>//Let user know if they've left an invalid email in the field
   $signupEmail.on("blur", function() {
      var form = new SignupForm($signupEmail.val());
          if ( !form.isEmailEmpty() &amp;&amp; !form.isEmailValid() ) {
              form.showEmailError("Invalid email");
          } else {
              form.isEmailRegistered();
              if (!form.emailError &amp;&amp; form.isEmailValid()) {
                  form.hideEmailError();
                  form.showValidEmail();
              }
          }   
  });
  </code></pre>
  
  <p>and:</p>
  
  <pre><code> //Let user know when they've entered a valid email
  $signupEmail.on("keyup", function() {
      var form = new SignupForm($signupEmail.val());
      var userEmail = form.userEmail();
       $.post("/sitename/scripts/checkEmailAvailability.php", {"userEmail": userEmail}, function (data) {
          if (form.isEmailValid() &amp;&amp; !data) {
              form.showValidEmail();
          } else {
              form.hideEmailError();
          }
      });   
  
  });
  </code></pre>
  
  <p>This and the rest of the code works flawlessly with my signup and contact forms.</p>
  
  <p>I also have a directory called <code>users/</code> with a sub-directory called <code>account-settings/</code>. Each page in the sub-directory contains a different form users can submit to update their account. For instance, there is an update-email.php, update-password.php, etc. These pages are loaded onto an account-settings.php page through an <code>xmlhttprequest()</code>. The problem I'm having is figuring out an efficient way of including the jquery real-time validation, which doesn't seem to work unless included in the <code>xmlhttp.onreadystatechange</code> function. As it is now, I have to include the jquery validation twice; One for the signup/contact forms and another for the account-settings forms. Please review the following code: 
  <br></p>
  
  <p><strong>Site/users/account-settings/account-settings.php</strong></p>
  
  <pre><code>//php includes
  //session configurations
   &lt;div id="user-settings"&gt;
          &lt;ul&gt;
              &lt;li&gt;&lt;a href="#update-email"&gt;Update Email&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="#update-password"&gt;Update Password&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="#update-photo"&gt;Update Photo&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="#manage-friends"&gt;Manage Friends&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/div&gt;
      &lt;div id="user-settings-forms"&gt;
  
      &lt;/div&gt;
  
  //php include footer
  </code></pre>
  
  <p><br></p>
  
  <p><strong>Site/javascript/jquery.js</strong></p>
  
  <pre><code>//Show User Profile's Settings Forms
  $("#user-settings li").on('click', function() {
      var data = $(this).find('a').attr('href');
      var xmlhttp = new XMLHttpRequest();
  
      xmlhttp.onreadystatechange = function() {
          if (xmlhttp.readyState === 4 &amp;&amp; xmlhttp.status === 200) {
              document.getElementById("user-settings-forms").innerHTML = xmlhttp.responseText;
              // I'm currently duplicating all of the real-time validation here
              // If I move the code anywhere else, it doesn't work
              // This makes for a lot of redundancy
              // I also don't know how this affects the xmlhttprequest
          }
      }
  
      if (data === '#update-email') {
          xmlhttp.open('POST', 'update-email.php');
      } else if (data === '#update-password') {
          xmlhttp.open('POST', 'update-password.php');
      } else if (data === '#update-photo') {
          xmlhttp.open('POST', 'update-photo.php');
      } else if (data === '#manage-friends') {
          xmlhttp.open('POST', 'manage-friends.php');
      }
  
      xmlhttp.send();
  
  });
  </code></pre>
  
  <p>Things I've tried so far: <br>
   1. Including the necessary jquery code on the update-email.php, update-password.php pages. I thought: well, if I include the code directly on the page that would be using it, it should always work regardless of whether the page is passed through an xmlhttprequest or not. But, I was wrong. It  doesn't work. <br>
  <br>
   2. Remove <code>require("templates/header")</code> from the update-email.php, update-password.php pages. This template includes, among other things, all the links to my css and js files. I thought: maybe there is a collision of the templates happening when the page is sent through the xmlhttprequest function. It only seemed half logical since the css was still rendering without a problem but, it was worth a shot. <br>
  <br>
   3. Including the javascript file asynchronously, the same way I would a <a href="https://developers.google.com/maps/documentation/javascript/examples/map-simple-async" rel="nofollow">Google map</a>. I thought: since the xmlhttprequest happens asynchronously, adding the script asynchronously may be necessary. But, that didn't work either.<br>
  <br></p>
  
  <p>So, is there a more efficient way of including the jquery events handlers that belong with the page passed through an xmlhttprequest object? If this is the only way, how does it affect the xmlhttprequest? Slow it down? No change at all? Will adding too much code cause a problem with the script in any way? Thank you. I really appreciate any help you can give. </p>