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?