2

I have this code in a javascript file to hide some search filters...

$('#views-exposed-form-faceted-search-page .form-type-select').once().hide();

The user can toggle the visibility of the filters by clicking on my #advanced-search link...

$('#views-exposed-form-faceted-search-page #advanced-search a').once('advanced_search').click(function(){
        $('#views-exposed-form-faceted-search-page .form-type-select').toggle();
});

It works, but when the AJAX form is submitted, the hide() function runs again and the .form-type-select elements are hidden. How do I prevent them from being hidden a second time? I only want my code to hide the selects when the page first loads.

  • Simple, don't put the hide code inside Drupal.behaviours – No Sssweat Apr 3 at 19:49
  • @NoSssweat That doesn't quite suffice, because then we have the opposite problem -- the filters will always show after the form is submitted. – arnoldbird Apr 4 at 15:10
2

If Ajax reloads this part you are hiding, the marker once() has set is overwritten, also the show/hide status. Then you need a different approach, either submit the show/hide status, so that it can be returned by Ajax, or create a show/hide Ajax form element to have full control over it through Ajax.

If this is outside of the Ajax wrapper you can do this in javascript, but apply the jQuery selectors on context:

$('#views-exposed-form-faceted-search-page .form-type-select', context).once('search-hide').hide();

See https://www.drupal.org/docs/8/api/javascript-api/javascript-api-overview

  • "If this is outside of the Ajax wrapper". The filters are provided by the views module, so I would have to inspect how the views module handles things, but it does appear the filters form is within the Ajax wrapper. At least, it behaves that way. "submit the show/hide status, so that it can be returned by Ajax". I am not sure how to do that, given that the views module is handling things. I may not pursue this further, as the toggle provides limited benefit compared to the development complexity. I think your answer is on the right track, though. – arnoldbird Apr 4 at 15:15

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.