I'm asking this question because I couldn't find an answer in another one. If there is please link it to me.
I have a jQuery Datepicker on which I set the parameter numberOfMonths: 2
I want it to be 1 if the screenSize goes below some number (eg 768px). I tried:
$(window).resize(function(){
if($(window).width() < 768){
$('#datepicker').datepicker({
numberOfMonths: 1
})
}
}
But since the datepicker is already initialised it does not work.
What can I do?
http://api.jqueryui.com/datepicker/#option-numberOfMonths
If the datepicker has already been initialized, you should use the options setters:
The parameter can also be a number, just like the way you initialize it. Check the link for more info.
You are on the right track, but there's enough things that could prove to be helpful that I wanted to post a full answer.
First, Here's a working Fiddle
Here's the code that I propose, with comments explaining each element:
If you're not familiar with the notion of "Debounce", see this article. The concept is that you prevent code from running on every single instance of an event. In this case, a resize of 500 pixels might trigger the "resize" event several hundred times, and if we did not "debounce", the datepicker function would get called several hundred times. Obviously we don't care about all the "interim" calls to datepicker, we really only care about the last one, which is triggered by the last resize event, which should reflect the ultimate size the user stopped at.