1

I have created multiple dates selection for multiple input fields using jQuery datepicker.

Now Problem is i want to delete any date form cursor positiondatepicker example

External Link to Jquery ui

<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">

Multile Date selection input fields

<input type="text" class="dates_opted dates_opted_1">
<br><br>
<input type="text" class="dates_opted dates_opted_2">    

jQuery and jQuery ui Js file cdn link

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> 

Js code

    <script>
    function cleanArray(actual) {
      var newArray = new Array();
      for (var i = 0; i < actual.length; i++) {
        if (actual[i]) {
          newArray.push(actual[i]);
        }
      }
      return newArray;
    }
    jQuery(document).ready(function($) {
      $('.dates_opted').each(function(index){
          var dates=[];
       $(this).datepicker({
      dateFormat: "yy-mm-dd",
      onSelect: function(){
       dates=cleanArray(dates);
        dates.push($(this).val());
        //console.log(dates);
        $(this).val(dates);


      },showAnim :'slide',
    minDate: 0

    }).keyup(function(e) {
        if(e.keyCode == 8 || e.keyCode == 46) {
          var datesString=$(this).val().split(",");
           dates=[];
          for(var i=0 ; i<datesString.length-1;i++){

            dates.push(datesString[i]);
          }
          $(this).focus();
          $(this).blur();
          $(this).val(dates);


        }
    });
         });
    });
</script>

Here is jsfiddle

0

1 Answer 1

0
function(e)
{
   if(e.keyCode == 8 || e.keyCode == 46)
      {
         var datesString=$(this).val().split(",");
         dates=[];

Get the cursors position and found the index of date you need to remove

      cix=$(this).selectionStart;
      ix=Math.floor(cix/9);

Run trough all array and skip that value in new array

      for(var i=0 ; i<datesString.length;i++)
         {
             if(i!=ix)
               {
                  dates.push(datesString[i]);
               }
         }
      $(this).focus();
      $(this).blur();
      $(this).val(dates);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.