1

I'm trying to reset the value of the input once i click a date in the datepicker, my goal is to add a single date in the array once a click a date, what happening is once i click a date for the multiple times, it still holds the previous value of the input.

var date = new Date();
var days = [];

$(document).ready(function(){
  $("#inputMultiDate").datepicker({
    autoclose: true,
    todayHighlight: true,
    multidate: true,
    format: 'dd-mm-yyyy'
  }).on("changeDate", function(){
    dates = $('#inputMultiDate').val().split(',');
    days.push(dates);
     $('#inputMultiDate').attr('value', '');  
    console.log(days);
  });;
});

<input type="text" id="inputMultiDate" class="form-control" placeholder="Pick the multiple dates">

3 Answers 3

1

You can easily access the event by using datepicers onSelect:

var date = new Date();
var days = [];

$(document).ready(function() {
  $("#inputMultiDate").datepicker({
    autoclose: true,
    todayHighlight: true,
    multidate: true,
    format: 'dd-mm-yyyy',
    onSelect: function(dateText) {
      dates = $('#inputMultiDate').val().split(',');
      days.push(dates);
      $('#inputMultiDate').attr('value', '');
      console.log(days);
      let value = $("#inputMultiDate").datepicker("getDate");
      console.log(value);
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<input type="text" id="inputMultiDate" class="form-control" placeholder="Pick the multiple dates">

Sign up to request clarification or add additional context in comments.

5 Comments

may i know what's the difference between "on" and "onSelect"?
.on() listens to the events of the DOM, onSelect is the event listener specifically for the datePicker as defined here. The documentation states, that the datepicker selection doesn't fire any events to the outside. Take a look at the api and see the column events.
So instead of binding the datepicker $("#inputMultiDate") to an event, you can add an event handler directly to the specific selection event.
i also wan't to know why does the input holds the previous value even i use the attr.val('') or .val('[)?
If you want to get the current value of datepicker, use let value = $("#inputMultiDate").datepicker("getDate"); instead
0
$('#inputMultiDate').val('')

In jQuery "val" is a get function also a set function. You can set your input with val('').

1 Comment

i already tried this one, but the problem is it still holds the previous date of the input and store it into my array, as a result ex 0:["02-01-2019"] 1:["02-01-2019", "03-01-2019"]
0

Try to call the new Date() function every time the date changes. Currently, you have one date as the page loads.

var date = new Date();
var days = [];

$(document).ready(function(){
  $("#inputMultiDate").datepicker({
    autoclose: true,
    todayHighlight: true,
    multidate: true,
    format: 'dd-mm-yyyy'
   }).on("changeDate", function(){
    date = new Date();
    dates = $('#inputMultiDate').val().split(',');
    days.push(dates);
    $('#inputMultiDate').attr('value', '');  
    console.log(days);
  });;
});

1 Comment

same output sir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.