Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Datepicker onClose select next

I have a system where people can add multiple from and to dates ie screenshot: http://screencast.com/t/Q7MHQVLnn4J9

On selecting the first date the onClose event sets the date for second input and opens it. IE

datePickers = function() {
    $(".from_date").datepicker({
      minDate: 'D',
      dateFormat: "dd/mm/yy",
      defaultDate: "+1w",
      numberOfMonths: 2,
      onClose: function(selectedDate) {
        $(".to_date").datepicker("option", "minDate", selectedDate);
        return $(".to_date").datepicker("show");
      }
    });
    return $(".to_date").datepicker({
      minDate: '+1D',
      dateFormat: "dd/mm/yy",
      defaultDate: "+1w",
      numberOfMonths: 2
    });
  };

My problem becomes when I have 2,3 or more and editing the first or second one.... On close it opens the last input.

I have tried using .next('to_date') and closest('to_date') but its not working.

Any advise on how I can get around this.

As requested a fiddle: http://jsfiddle.net/FdfPY/

like image 280
Lee Avatar asked Mar 11 '13 15:03

Lee


1 Answers

Rather than using return $(".to_date").datepicker("show");, do the following:

$(".from_date").datepicker({
      minDate: 'D',
      dateFormat: "dd/mm/yy",
      defaultDate: "+1w",
      numberOfMonths: 2,
      onClose: function(selectedDate) {
        $(".to_date").datepicker("option", "minDate", selectedDate);
        $(this).parent().next().children().focus();
      }
    });
 $(".to_date").datepicker({
      minDate: '+1D',
      dateFormat: "dd/mm/yy",
      defaultDate: "+1w",
      numberOfMonths: 2
    });

DEMO: http://jsfiddle.net/FdfPY/1/

EDIT:

If it is nested, then use the following:

$(".from_date").datepicker({
      minDate: 'D',
      dateFormat: "dd/mm/yy",
      defaultDate: "+1w",
      numberOfMonths: 2,
      onClose: function(selectedDate) {
        $(".to_date").datepicker("option", "minDate", selectedDate);
        $(this).parents('.span2').next().children().find('.to_date').focus();
      }
    });
 $(".to_date").datepicker({
      minDate: '+1D',
      dateFormat: "dd/mm/yy",
      defaultDate: "+1w",
      numberOfMonths: 2
    });

DEMO: http://jsfiddle.net/FdfPY/5/

like image 77
Dom Avatar answered Sep 20 '22 15:09

Dom