title: datepicker not working after loading page via ajax
body: <p>I'm using asp.net mvc 2 with jquery 1.10.1 and jquery ui 1.10.3.</p>
  
  <p>When I'm loading a page with $.ajax, the datepicker is a bit off. It will pop up and go back away just as usual, but the interaction with it is broken.</p>
  
  <p>For example: I click the input => datepicker pops up => select a date => error that currentdate is undefined</p>
  
  <p>A page can have multiple inputs with datepickers, which is why I'm using a .each in my code.</p>
  
  <p>I have 2 pages: Index.aspx and PatchDef.aspx. Index uses a $.ajax call to load in PatchDef.aspx dynamically.</p>
  
  <p>In PatchDef.aspx I am using a  tag with $(document).ready code which binds the datepickers to my inputs.</p>
  
  <p>Here's the code that I'm using right now:</p>
  
  <pre><code>$(document).ready(function () {
      $(".datePickerPatchDef").each(function () {
                      var dateFormat = $(this).attr("dateFormatPatchDef").toLowerCase();
                      $(this).datepicker({
                          dateFormat: dateFormat,
                          changeMonth: true,
                          changeYear: true
                      });
     });
  });
  </code></pre>
  
  <p>I have also tried the following code which I found <a href="https://stackoverflow.com/questions/11415614/datepicker-not-working-in-ajax-loaded-page">here</a> :</p>
  
  <p>NOTE: here a class will be something like dpCount1; dpCount2, ...</p>
  
  <pre><code>$(".datePickerPatchDef").each(function () {
                  var dateFormat = $(this).attr("dateFormatPatchDef").toLowerCase();
                  $(this).datepicker({
                  var dpCountClass;
                  var allClasses = $(this).attr("class").split(' ');
  
                  for (var i = 0; i &lt; allClasses.length; ++i) {
                      var singleClass = allClasses[i];
                      if (singleClass.match("^dpCount")) {
                          dpCountClass = "." + singleClass;
                      }
                  }
  
                  $("#switchUser").text(dpCountClass);
                  $("body").on("focusin", dpCountClass, function () {
                      $(this).datepicker({
                          dateFormat: dateFormat,
                          changeMonth: true,
                          changeYear: true
                      });
                  });
              });
          }
  </code></pre>
  
  <p>I thought the second try would solve the dynamic loading, but it just doesn't.</p>
  
  <p>The exact error I'm getting is:</p>
  
  <blockquote>
    <p>Microsoft JScript runtime error: Unable to set value of the property
    'currentDay': object is null or undefined</p>
  </blockquote>
  
  <p>and it goes to the code</p>
  
  <pre><code>o.selectedDay=o.currentDay=t("a",n).html()
  </code></pre>
  
  <p>where o = undefined</p>
  
  <p>So if you have any suggestions, I can always use them.
  If you need additional information, please ask!</p>