2

I am attempting to disable 2 inputs within the Views exposed filter form upon initial page load. However I don't want this to happen after the filter form is autosubmitted. I have the following code, but it runs every time the filters are autosubmitted.

(function ($){

Drupal.behaviors.ios2 = {
    attach: function (context, settings) {

        $('#edit-arrival-date-datepicker-popup-1, #edit-departure-date-datepicker-popup-1', context).once('disable-dates', function(){
            console.info('Disable em once');
            $(this).attr('disabled', true);
        });
    }
};


})(jQuery);

After the initial page load I can see the disable-dates-processed class on the input in question, but the filter autosubmit causes the attribute change to run again.

What is it that I'm getting wrong ?

1

The once method is a simple beast - it checks for a class of NAME-processed on the element (NAME being the string arg you pass it). If it doesn't find that class, it assumes this is the first execution; it runs the function, and adds the class to the element. When the code executes again, the class is there, so it doesn't run the function.

When your behaviour executes first time, there's no -processed class on the elements, so it gets added and your function executes. Everything's happy...

Then an AJAX call happens, and the textfields get replaced; they no longer have the -processed class on them, since they're new elements received from the from builder function, and once has no option but to start from scratch again, running the function and adding the class to the new elements.

2 ways you could solve this:

  1. Keep using behaviours, and use your own flag:

    (function ($) {
      var processed = false;
    
      Drupal.behaviors.ios2 = {
        attach: function (context, settings) {
          if (!processed) {
            $('#edit-arrival-date-datepicker-popup-1, #edit-departure-date-datepicker-popup-1', context).attr('disabled', true);
            processed = true;
        }
      };
    
    })(jQuery);
    
  2. Conclude that behaviours aren't appropriate for this particular op, and stick with jQuery.ready

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.