Josel Parayno
Josel Parayno

Reputation: 181

how to reset input value attribute

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">

Upvotes: 1

Views: 163

Answers (3)

Stephan T.
Stephan T.

Reputation: 6074

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">

Upvotes: 1

F. Ception
F. Ception

Reputation: 35

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);
  });;
});

Upvotes: 0

Onurgule
Onurgule

Reputation: 717

$('#inputMultiDate').val('')

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

Upvotes: 0

Related Questions