Search code examples
javascriptjqueryhtmljquery-uijquery-multidatespicker

How to get total of days Multidatepicker MDP


I using Multidatepicker MDP from here.
I want to get the total days that selected by user, and place it into input text.

For example if the user select 2 days, then the total is 2.
And the total put in a input type text.

Here is my JS

$('#mdp-demo').multiDatesPicker({
     numberOfMonths: [1,2],
     altField: '#altField',
     minDate: 2,
});

Thank you for your answers


Solution

  • Using the information from the site you linked: http://dubrox.github.io/Multiple-Dates-Picker-for-jQuery-UI/#demo-ui-calendar-methods

    You can specify the onSelect method to respond to selections.
    To get an array of selected dates you can use $('#mdp-demo').multiDatesPicker('getDates') as documented.

    If you only care for the number of selected dates you can simply use the .length of the array.

    I never worked with this component before but parsing together the info from the documentation the below seems to work. Adjust as needed.

    $('#mdp-demo').multiDatesPicker({
        numberOfMonths: [1, 2],
        altField: '#altField',
        minDate: 2,
        onSelect: function(){
          $('#numberSelected').val($('#mdp-demo').multiDatesPicker('getDates').length);
        }
      });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link href="https://code.jquery.com/ui/1.12.1/themes/pepper-grinder/jquery-ui.css" rel="stylesheet" />
    <link href="https://cdn.rawgit.com/dubrox/Multiple-Dates-Picker-for-jQuery-UI/master/jquery-ui.multidatespicker.css" rel="stylesheet" />
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
    <script src="https://cdn.rawgit.com/dubrox/Multiple-Dates-Picker-for-jQuery-UI/master/jquery-ui.multidatespicker.js"></script>
    <div>
    <span>Number Of Days Selected</span>
    <input id="numberSelected" type="text"/>
    </div>
    <br/>
    <div id="mdp-demo"></div>