0

Not sure what I am doing incorrectly. I add my datepicker class when the page loads.

 $(function()
 {
      $( ".datepicker" ).datepicker({changeMonth: true, changeYear: true, dateFormat: "yy-mm-dd", yearRange: "2002:"});
 });

I want to change my datepicker class to

   $( ".datepicker" ).datepicker({changeMonth: true, changeYear: true, dateFormat: "yy-mm-dd", yearRange: "2010:"});

On event I remove the class then unbind the datepicker, then rebind.

    $("#datefield").removeClass("datepicker hasDatepicker");
    $('#datefield').unbind();
    $('#datefield').bind();

    //rebind
    $("#datefield").datepicker();

how can I rebind the new datepicker? I've removed the datepicker class then re-added the class. Nothing seems to work.

1
  • I think I've got it correct. $("#datefield").datepicker({changeMonth: true, changeYear: true, dateFormat: "yy-mm-dd", yearRange: "1998:"});. How can I add this using a class? Commented May 2, 2018 at 11:14

1 Answer 1

1

You simply have to store the element that you have initialized datepicker on, and then update its options using its own API thereafter:

$(function() {
  var $dateField = $("#datefield").datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: "yy-mm-dd",
    yearRange: "2002:"
  });
  
  $('button').on('click', function() {
    $dateField.datepicker("option", { yearRange: "2010:" });
    console.log('Updated. Try opening datepicker again now');
  });
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>

<p>Date: <input type="text" id="datefield"></p>
<button type="button">Update year range to <code>2010:</code></button>

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, didn't think about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.