Reset Exposed Views Filter Select Options with Javascript

Last modified: 
Wednesday, December 2nd, 2015

A javascript to reset select fields created by Views exposed filters.

/**
 * Resets options elements in Drupal exposed filters.
 * @param el String
 *  Start searching for options in this element.
 * @param default_value String
 *  Reset the field to the option whose value attribute matches default_value.
 */
function clearDrupalSelectOptions(el, default_value) {
    var options = el.find("option");
    var widget = options.closest(".views-widget");
    options.each(function() {
        var option = $(this);
        option.removeAttr("selected");
        if (option.val() == default_value) {
            option.attr("selected", true);
            widget.find(".chosen-single span").text(option.text());
        }
    });
}


The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.