0

I am trying to add a reset button to some exposed filters for my view. My code:

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

      var reset = '<div class="form-item js-form-item form-group" style="padding: 26px 0 0 10px;"><a class="btn btn--lg" id="blog-filter-reset">Reset</a></div>';
      $('.form-item-field-term-center-target-id').once().after(reset);

      $('#blog-filter-reset').on('click', function(e){
        $('#edit-combine').val('');
        $('#edit-field-term-region-target-id').val('All');
        $('#edit-field-term-center-target-id').val('All');
      });

    }
  };

})(jQuery, Drupal);

This behavior does not get attached to the AJAX-loaded output, and so the reset button doesn't work. When I research how to do this, the advice I find is often along these lines:

Just add this in the success callback when you’ve finished manipulating the DOM:

Drupal.attachBehaviors();

However, how do I know what the success callback is for the exposed views filters? What is this actually going to look like in my javascript code?

Questions about Drupal behaviors appear many times on stackexchange, but these questions often go unanswered, or there is no accepted answer, or the answer given is incomplete or ambiguous. Sometimes you will see something vague like "just attach the behaviors to the container where the AJAX content is loaded." But how do I know what that container is if the AJAX is handled by views?

Your Answer

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

Browse other questions tagged or ask your own question.