title: Destroy and Set jquery datepicker on condition
body: <p>I am using Jquery with ASP.NET MVC and create one page that has some fields to enter the data and have some condition based functionality.</p>
  
  <p>Like I have option to choose Job Order Date in selection equals to, not equals to , today, tomorrow , etc... </p>
  
  <p><a href="https://i.stack.imgur.com/JM0Ah.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JM0Ah.png" alt="enter image description here"></a></p>
  
  <p>So I have to set the right side textbox as datepicker or some time only textbox based on selection of the dropdownlist value.</p>
  
  <p>If from first till greater than equals to I want textbox as datepicker and on selection of the last two options i.e. in last x days and in next x days I want to only textbox not datepicker I mean I want to destroy the datepicker.</p>
  
  <p><a href="https://i.stack.imgur.com/wSplN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wSplN.png" alt="enter image description here"></a></p>
  
  <p>I have done following code but not getting the result.</p>
  
  <pre><code>function checkselection(obj) {
              if ($("#ddl_" + obj + " :selected").text() == "in last x days" || $("#ddl_" + obj + " :selected").text() == "in next x days") {
                  removedatepicker(obj);
                  $("#txt_" + obj).attr("disabled", false);
              }
              else {
                  setdatepicker(obj);
                  $("#txt_" + obj).attr("disabled", false);
              }
          }
  
      function setdatepicker(id) {
                  $('#' + id).datepicker({
                      autoclose: true
                  });
              }
  
              function removedatepicker(id){
                  $('#' + id).datepicker("remove");
              }
  </code></pre>
  
  <p>obj is the id of the textbox.</p>