Rob Tucker Rob Tucker - 3 years ago 142
Javascript Question

Combining jQuery datepicker functions

I have two functions that I'd like to use to narrow down the selection in my jQuery Datepicker. I'm stuck on how to combine these two functions so I can use them in combination with each other. Right now which ever one I put first takes priority and the other doesn't work.

//the html
<input type="text" class="input-text hasDatepicker" name="order_delivery_date" id="datepicker" placeholder="Select Date" value="">


//two functions, whichever one is first works
<script type="text/javascript">
jQuery(function ($) { $( "#datepicker" ).datepicker({ minDate: "+1", maxDate: "+1M +10D" }); });
jQuery(function ($) { $( "#datepicker" ).datepicker({beforeShowDay: function(date){ return [date.getDay() != 2, '']; }})});
</script>


// my attempt to combine them, not working
<script type="text/javascript">
jQuery(function ($) {
$( "#datepicker" ).datepicker({ minDate: "+1", maxDate: "+1M +10D" });
$( "#datepicker" ).datepicker({beforeShowDay: function(date){ return [date.getDay() != 2, '']; }});
});
</script>

Answer Source

The first line of code already creates a datepicker object on $('#datepicker') DOM element. You should pass all options to the datepicker all in one go:

$('#datepicker').datepicker({ minDate: "+1", maxDate: "+1M +10D" , beforeShowDay: function(date){ return [date.getDay() != 2, '']; }});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download