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.

2
  • Simple, don't put the hide code inside Drupal.behaviours
    – No Sssweat
    Commented Apr 3, 2019 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
    Commented Apr 4, 2019 at 15:10

1 Answer 1

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

1
  • "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
    Commented Apr 4, 2019 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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