Using jQuery's Datepicker to show only 3 weekdays

258 views Asked by At

i'm quite new to javascript and code overall. I'm using a datepicker at my wordpress block and i created this little code (after searching stackoverflows forums) and it serves me well to what i needed so far:

$("#input_4_14").datepicker({
    minDate: 1,
    beforeShowDay: function (d) {
        return (1 == d.getDay() ? [true, ''] : [false, '']);
    }
});

input_x_y refers to the datepicker i'm using at the form(x) and the datepicker(y).

What it does (and does correctly) is limit the past days and shows only monday.

What i don't know how to do is to show only monday, friday and saturday. I tried doing:

1,5,6==d.getday() but it doesn't work.

Could anyone give me a hand here?

UPDATE: I redid the code like this:

<script type='text/javascript'>
var days = [1, 5, 6];
jQuery.noConflict(); 
  jQuery(document).ready(function($) {
$("#input_4_14").datepicker({
    minDate: 1,
    beforeShowDay: function (d) {
        return [$.inArray(d.getDay(), days) > -1, ''];
    }
});
  });
</script>

but it still doesn't work. Side note: i'm using Wordpress with HTML Javascript widget.

1

There are 1 answers

3
Arun P Johny On

You can store the days you want to allow in an array an then use $.inArray() to test whether the current day is a valid one

jQuery(function ($) {
    var days = [1, 5, 6];
    $("#input_4_14").datepicker({
        minDate: 1,
        beforeShowDay: function (d) {
            return [$.inArray(d.getDay(), days) > -1, ''];
        }
    });
});

Demo: Fiddle