0

I am using a Bootstrap DatePicker so user can select the dates and at the back end, the selected dates will be stored in an array.

Whenever I select the dates, the javascript code .push() is used to capture the selected dates and append the selected dates in the array.

I believe this is the correct format for an array right?

DateSelected = ["April 1, 2021", "April 2, 2021", "April 3, 2021", "April 4, 2021"]

BUT what is happening is this:

DateSelected = ["April 1, 2021, April 2, 2021, April 3, 2021, April 4, 2021"]

So what happening here is its as if the array contains ONE (1) entry only whenever I console.log(DateSelected.length). It should suppose to return FOUR (4) since I selected 4 dates.

Here's my code:

Bootstrap Date Picker

$('.datepicker').datepicker({
    multidate: true,
    toggleActive: true
}).on('changeDate', function(e){
    $('#dt_due').val(
       $('.datepicker').datepicker('getFormattedDate') 
    );
});

Javascript/ jQuery

var DateInsured = [];
DateSelected.push($('#dt_due').val());

console.log(DateSelected)
console.log(DateSelected.length)

Here's a screenshot of the Google Chrome Console

enter image description here

Ben Daggers
  • 1,000
  • 1
  • 16
  • 51
  • `DateSelected = $('#dt_due').val().split(",")` – adiga Apr 27 '21 at 09:22
  • @adiga, can you post this as an answer. it technically solve my problem so I can reward you properly... Thank you very much! – Ben Daggers Apr 27 '21 at 09:29
  • That's okay. I've closed as a duplicate. The datepicker returns a single string of all the dates. You need to [`split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) that string at `,` character to get an array of each individual date strings. – adiga Apr 27 '21 at 09:31
  • oh i see. thank you very much @adiga! Have a great day! – Ben Daggers Apr 27 '21 at 09:32

0 Answers0