5

I want to set multiple dates programmatically on an inline jQuery datepicker. I have an array of dates and I want to loop through them and on each iteration, a date should be selected in datepicker.

The result should be multiple dates selected on the inline jQuery datepicker.

This is what I'm trying, but I haven't had much success with it.

for(var j=0; j<dateArr.length; j++){
    $("#inlineDp").datepicker.('setDate',dateArr.pop());
}
AstroCB
  • 12,337
  • 20
  • 57
  • 73
Superman
  • 871
  • 2
  • 13
  • 31

1 Answers1

3

That second part isn't a method: it's part of the datepicker constructor.

What you need to do is loop through your array and use that constructor each time (but initialize the datepicker before the loop).

As for selecting multiple dates, you can't do that by default (only one date can be selected at one time), so you'd have to use a third party plugin.

$("#inlineDp").datepicker();

for (var j = 0; j < dateArr.length; j++) {
    window.setTimeout(function(){
       $("#inlineDp").datepicker("setDate", dateArr[j]);
    }, 500);
}

(Note that for simplicity, this code does not take into account the problem with timeouts in loops (namely, that they don't work). However, the code in the demo below does (so you should use the code there instead of that from explanatory example above).)

Demo

Community
  • 1
  • 1
AstroCB
  • 12,337
  • 20
  • 57
  • 73
  • 1
    Thanks Astro for your answer. My array is in the following form. And I'm not getting above script even working with this array. Please guide. dateArr = [Wed Dec 03 2014 00:00:00, Wed Dec 10 2014 00:00:00, Wed Dec 17 2014 00:00:00] – Superman Dec 13 '14 at 15:51
  • @Superman Try wrapping each of those in a Date constructor in the loop [like this](http://jsfiddle.net/AstroCB/zop24xvf/1/). – AstroCB Dec 13 '14 at 15:56
  • Yeah cool it is working. The last thing that I need is all the dates should have to be selected at once. So that later on I can save these dates to DB. In our case Dec3, Dec10 and Dec17 have to be set. – Superman Dec 13 '14 at 16:04
  • I used it already with the same code like this. $('#inlineDp').multiDatesPicker('addDates', dateArr[j]); But the behavior was same, it also selected the last index date. – Superman Dec 13 '14 at 16:14
  • @Superman Substitute that line for the code in the demo and it should work (don't forget to add the library in external resources). – AstroCB Dec 13 '14 at 16:15
  • It is working the same way. It selected 3, 10 then 17. So at last when you will getDate from this picker. It will give us only 17. It should have to select 3, 10 and 17 at a time. So that we can have 3 dates selected on the picker. I'm very thankful to you for giving me time. Thanks. – Superman Dec 13 '14 at 16:35
  • @Superman It did; look for the yellow outline around the selected dates. (And no problem, I'm glad to be able to help.) – AstroCB Dec 13 '14 at 16:36
  • Oh yes. It is there. :D Thanks a lot Astro. Give me your website link. It will soon be acknowledged. It looks bad to accept but it is truth that I spent almost more than 6 hours on it. And now it is fixed by you. Thanks a lot. I would appreciate if you can tell me some good resources to learn jquery. – Superman Dec 13 '14 at 16:40
  • @Superman It really is no problem; I'm glad I could help you out and I hope you're able to move on with your project now. Good luck. (Also, if you want to see how to get the all of the dates after they've been selected, take a look at [this](http://jsfiddle.net/AstroCB/zop24xvf/4/).) As for resources, just skimming through the documentation is the best way to go. – AstroCB Dec 13 '14 at 16:43