1

I am trying to set a minDate in a jquery-ui datepicker. The date is retrieved using jquery from a data attribure of a parent node.

<p data-min-date="2015-03-10">Date: <input id="datepicker" type="text"> </p> <script> $("#datepicker").datepicker({ minDate: $(this).closest('p').data('minDate'), dateFormat: 'yy-mm-dd' }); </script> 

You can check this fiddle to see that the minDate is not considered by the datepicker.

Can you spot what I'm doing wrong?

1 Answer 1

2

The issue is because $(this) in the scope of the datepicker intialiser does not equal the element which was selected. To do what you require, you need to wrap the datepicker() call in an each() and iterate over every element of the selector:

$("#datepicker").each(function() { $(this).datepicker({ minDate: $(this).closest('p').data('minDate'), dateFormat: 'yy-mm-dd' }); }); 

Updated fiddle

Alternatively, if the datpicker will only ever be created on a single element, you can select it directly for the minDate property:

$("#datepicker").datepicker({ minDate: $("#datepicker").closest('p').data('minDate'), dateFormat: 'yy-mm-dd' }); 

Updated fiddle

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

6 Comments

how we can add mindate as current date? so that will automatically update.
@GaneshAher minDate: new Date()
minDate: new Date() is not working. $('#datepicker5').datepicker({ autoclose: true, //changeYear: true, minDate: new Date(), });
Yes it does: jsfiddle.net/D4AGz/1287. Check your console for errors. If you still cannot get it to work I'd suggest you read the jQueryUI documentation, or start a question about it.
Please go through stackoverflow.com/questions/47768636/… this question. Thanks
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.