You are viewing limited content. For full access, please sign in.

Question

Question

Hide previous dates javascript

asked on February 9, 2017 Show version history

hi everyone.

i have a form where the previous dates from today must be hidden in the first date picker and the second date picker must not show dates previous to the first selected date.

 

 

The form is working for the first row but i can't get the code to work for the other rows that follow when i "add" a new row. 

Can anyone assist me with this Please?

here is my current code :

 

$(document).ready(function(){

  function updateMinimumEndDate () 
  {
    var minimum = $('.DepartDate input').val();
    var minSplit = [];
    minSplit = minimum.split("/");
    var newMin = (minSplit[2]+"-"+minSplit[0]+"-"+minSplit[1]);
    $('.ReturnDate input').attr('min',newMin);
  }
  $('.DepartDate input').change(updateMinimumEndDate);

});
$(function() {

  $(document).ready(function () {

   var todaysDate = new Date(); 
    
    var year = todaysDate.getFullYear(); 						
    var month = ("0" + (todaysDate.getMonth() + 1)).slice(-2);	
    var day = ("0" + todaysDate.getDate()).slice(-2);			

   	var minDate = (year +"-"+ month +"-"+ day);  
    
    $('.DepartDate input').attr('min',minDate);
   
  });
});

 

0 0

Answer

SELECTED ANSWER
replied on February 10, 2017

When you add a new row, you need to update the attribute of the new field.

Use script like this:

$(document).ready(function(){

  function updateMinimumEndDate () 
  {
    $('.ReturnDate input').each(function(index,elm){
      var minimum = $(elm).closest('tr').find('.DepartDate input').val();
      var minSplit = [];
      minSplit = minimum.split("/");
      var newMin = (minSplit[2]+"-"+minSplit[0]+"-"+minSplit[1]);
      $(elm).attr('min',newMin);
    });
  }
  var todaysDate = new Date(); 
  var year = todaysDate.getFullYear(); 						
  var month = ("0" + (todaysDate.getMonth() + 1)).slice(-2);	
  var day = ("0" + todaysDate.getDate()).slice(-2);			
  var minDate = (year +"-"+ month +"-"+ day);
  
  $('.DepartDate input').attr('min',minDate);
  $('.DepartDate input').change(updateMinimumEndDate);
  $('.cf-table-add-row').click(function() {
    $('.DepartDate input').change(updateMinimumEndDate);
    $('.DepartDate input').attr('min',minDate);
  });

});

 

1 0
replied on February 10, 2017

Hi Rui

it works perfectly now!

 

Thanks a lot, I appreciate your help!!!!

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.