0

I have dynamically added datepicker textboxes of id's TrainingPeriodStart[] and TrainingPeriodEnd[]. I need to target each pair to set their proper start and end date.

The setting of the start and end date is found on the documentation link here.

I have the code below in mind but it does not work:

    var inputName = '';
    var number = '';

    $('input[id^="TrainingPeriodStart"]').click(function() {
        inputName = $(this).attr('id');
        number = inputName.substr(inputName.length - 3); // get [number]
    }).datepicker({
        changeYear: true,
        changeMonth: true,
        dateFormat: 'yy-mm-dd',
        onSelect: function( selected ) {
            $('#TrainingPeriodEnd'+number).datepicker( "option", "minDate", 
                selected )
        }
    });

    $('input[id^="TrainingPeriodEnd"]').click(function() {
        inputName = $(this).attr('id');
        number = inputName.substr(inputName.length - 3); // get [number]
    }).datepicker({
        changeYear: true,
        changeMonth: true,
        dateFormat: 'yy-mm-dd',
        onSelect: function( selected ) {
            $('#TrainingPeriodStart'+number).datepicker( "option", "maxDate", 
                selected )
        }
    });

Basically I want to target datepicker pairs with their [number] attribute and setting their minimum and maximum dates to restrict the user from selecting illegal date ranges.

How do I solve this?

JeffB
  • 1,887
  • 15
  • 13
  • 1
    Works for me: http://jsfiddle.net/mccannf/SCXMD/2/ . What do you mean "does not work?". – mccannf Feb 07 '13 at 12:24
  • @mccannf: Thank you very much for the confirmation. I actually use a cloning script for these datepicker pairs like here http://stackoverflow.com/a/4400887/1685185. The datepicker works but not the actual start/end events :( Does cloning these affect? –  Feb 10 '13 at 12:16

1 Answers1

1

If you clone elements, you need to add new click events to the cloned elements in order for the event to be triggered:

$(function() {
    var inputName = '';
    var number = '';

    var template = $('#training .trainingperiod:first').clone(),
        trainingPeriodCount = 1;

    var addTrainingPeriod = function(){
        trainingPeriodCount++;
        var trainingPeriod = template.clone().find(':input').each(function(){
            var newId = this.id.substring(0, this.id.length-1) + trainingPeriodCount;
            this.name = this.id = newId; // update id and name (assume the same)
        }).end() // back to .trainingperiod
        .attr('id', 'TrainingPeriod' + trainingPeriodCount) // update attendee id
        .prependTo('#training'); // add to container

        $('#TrainingPeriod' + trainingPeriodCount).find('input[id^="TrainingPeriodStart"]').datepicker({
            changeYear: true,
            changeMonth: true,
            dateFormat: 'yy-mm-dd',
            onSelect: function( selected ) {
                var thisInput = $(this).attr('id');
                var thisNumber = thisInput.substr(thisInput.length - 3);
                $('#TrainingPeriodEnd'+thisNumber).datepicker( "option", "minDate", 
                    selected )
            }
        });
        $('#TrainingPeriod' + trainingPeriodCount).find('input[id^="TrainingPeriodEnd"]').datepicker({
            changeYear: true,
            changeMonth: true,
            dateFormat: 'yy-mm-dd',
            onSelect: function( selected ) {
                var thisInput = $(this).attr('id');
                var thisNumber = thisInput.substr(thisInput.length - 3);
                $('#TrainingPeriodStart'+thisNumber).datepicker( "option", "maxDate", 
                    selected )
            }
        });        
    };

    $('.add').click(addTrainingPeriod); // attach event

    $('input[id^="TrainingPeriodStart"]').click(function() {
        inputName = $(this).attr('id');
        number = inputName.substr(inputName.length - 3); // get [number]
    }).datepicker({
        changeYear: true,
        changeMonth: true,
        dateFormat: 'yy-mm-dd',
        onSelect: function( selected ) {
            $('#TrainingPeriodEnd'+number).datepicker( "option", "minDate", 
                selected )
        }
    });

    $('input[id^="TrainingPeriodEnd"]').click(function() {
        inputName = $(this).attr('id');
        number = inputName.substr(inputName.length - 3); // get [number]
    }).datepicker({
        changeYear: true,
        changeMonth: true,
        dateFormat: 'yy-mm-dd',
        onSelect: function( selected ) {
            $('#TrainingPeriodStart'+number).datepicker( "option", "maxDate", 
                selected )
        }
    });
});

Fiddle is here.

mccannf
  • 16,619
  • 3
  • 51
  • 63