Dependent date_popup maxDate In a profile2 profile, I want to change the maxDate of a date field depending on the selected value of gender field.

I am using a custom module implementing the hook_form_profile2_edit_PROFILE_TYPE_form_alter() and loading JavaScript. The date field uses the datepicker widget and the gender field is set as radios.

When the gender is selected as female, the maxDate should be set to "-18y". When selected as male, the maxDate should be set to "-21y".

Using the JavaScript, when a gender is selected the first time, the maxDate is set correctly. But, when the gender is changed, the maxDate does not change. It remains set at the first value. When the gender is changed, the maxDate should change too.

Here is my JavaScript:

(function($){
Drupal.behaviors.meh_profile = {
    attach: function (context, settings) {
        $('input:radio[name="profile_main[field_profile_gender][und]"]', context).change(function(){
            var genderVal = $('input:radio[name="profile_main[field_profile_gender][und]"]:checked').val();

            if (genderVal == Drupal.settings.gender_options.gender_female) {
                for (var id in Drupal.settings.datePopup) {
                    Drupal.settings.datePopup[id].settings.maxDate = '-18y';
                }
            } // if female

            if (genderVal == Drupal.settings.gender_options.gender_male) {
                for (var id in Drupal.settings.datePopup) {
                    Drupal.settings.datePopup[id].settings.maxDate = '-21y';
                }
            } // if male

        }); // gender change handler
    }
};
})(jQuery);


What am I missing?
Solved!

Instead of trying to change the maxDate using Drupal.settings.datePopup[id].settings, pass it as options to datepicker using the jQuery Datepicker syntax $( ".selector" ).datepicker( "option", { name: value } );

Here is the solved JavaScript code:

(function($){
Drupal.behaviors.meh_profile = {
attach: function (context, settings) {
    $('#edit-profile-main-field-profile-date-of-birth-und-0-value-datepicker-popup-0').attr('disabled', 'disabled');
    $('input:radio[name="profile_main[field_profile_gender][und]"]', context).change(function(){
        var genderVal = $('input:radio[name="profile_main[field_profile_gender][und]"]:checked').val();
        if (genderVal == Drupal.settings.gender_options.gender_female) {
            $("#edit-profile-main-field-profile-date-of-birth-und-0-value-datepicker-popup-0").val('').removeAttr('disabled').removeClass('hasDatepicker').datepicker({
                changeMonth: true,
                changeYear: true,
                yearRange: '-75:-18',
                maxDate: '-18y'
            });
        } // if female
        if (genderVal == Drupal.settings.gender_options.gender_male) {
            $("#edit-profile-main-field-profile-date-of-birth-und-0-value-datepicker-popup-0").val('').removeAttr('disabled').removeClass('hasDatepicker').datepicker({
                changeMonth: true,
                changeYear: true,
                yearRange: '-75:-21',
                maxDate: '-21y'
            });
        } // if male
    }); // gender change handler
}
};
})(jQuery);