Drupal 7 filter on change

There are several location that I offer to filter lists in Drupal 7. These locations tend to require that you select an option from a dropdown list, then, click a button that says “Filter”, “Apply” or other text. This is not a problem, but, some interfaces should just execute when the filter is selected, without clicking an apply button.

As an example, you may create a view that exposes a filter to the page a viewer will see. The visitor may want to select a filter and have the filter applied without having to do a second step of “applying” the filter. This may be accomplished by a quick chunk of jQuery. This chunk may be added as a module, drupal_add_js or simply added to an invisible block on the page.

For demonstration purposes, assume that you have a select filter with the id=edit-field-business-category-tid. To activate this filter on change of the selection use:


 jQuery("#edit-field-business-category-tid").change(function() {
     jQuery(this).parents("form").submit() ;
 }) ;

 

Leave a Reply