1

I am getting dates via ajax. And I want to disable those dates on my datepicker. But the parameter datesDisabled is not working in my code at all. It always shows the datepicker without disable any date. Below is my jquery code:

$(document).ready(function() {

    $("#people").blur(function() {
                var people = $("#people").val();
                if(people == "" || people == 0)
                    {
                        $("#people-error").html("Please enter number of people.");
                        return false;
                    }
                var experience_uid = $("#experience_uid").val();
                var req_url = "' . site_url() . '/wp-content/plugins/visitnorth/visitnorth.php";
                var ajax_req_for_schedule = 1;



               $.ajax({
                    async:false,
                    "type": "POST",
                    "url": req_url,
                    "data": {"people":people, "experience_uid":experience_uid, "ajax_req_for_schedule":ajax_req_for_schedule},
                    statusCode: {
                        404: function() {
                            alert("Oj, någonting gick fel. Försök igen.");
                        },
                        500: function() {
                            alert("Oops! An Internal Error has occurred.");
                        },
                    },
                    success: function (data) {
                       //output data is: ["11-08-2017, 11-05-2017, 11-03-2017"]
                       $(".datepicker").datepicker({
                            todayHighlight: true,
                            datesDisabled: data
                        });

                    }
                });

               });

            });

1 Answer 1

1

To disable some specific dates, you can override the method (beforeShowDay) will be called for each date and you can return the css class:

beforeShowDay: function (date){

                    if (date<minDate || date > maxDate) {
                        return 'invalid_date_datepicker';
                }
            }` 

In above example, invalid_date_datepicker is a css class, which grey outs the area.

and if you also want those grey out dates not be select , you can also override the onSelect and restrict it to select for those dates where this class has been applied.

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

3 Comments

I dont have any minDate or maxDate. The dates are coming from ajax dynamically.
You can re-configure the datepicker in success of that ajax result and can use the ajax response disable dates in beforeShowDay. You can check if disableDates.indexOf(date) >0 then disable the date otherwise not.
In case the answer helped you, could you please upvote it :).

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.