gform_chosen_options

Description

Allows the Chosen jQuery script settings for Drop Down and Multi-Select fields to be modified when the “Enable enhanced user interface” is checked. Fires when the form is loaded and has a field using Chosen.

Usage

 gform.addFilter( 'gform_chosen_options', function ( options, element ) {
    return options;
} );

Parameters

  • options JS Object

    The Chosen script options object. Additional documentation can be found in the Chosen 1.1.0 release.

    • allow_single_deselect bool

      When set to true on a single select, Chosen adds a UI element which selects the first element (if it is blank).

    • disable_search bool

      When set to true, Chosen will not display the search field (single selects only).

    • disable_search_threshold integer

      Hide the search input on single selects if there are fewer than (n) options.

    • enable_split_word_search bool

      By default, searching will match on any word within an option tag. Set this option to false if you want to only match on the entire text of an option tag.

    • inherit_select_classes bool

      When set to true, Chosen will grab any classes on the original select field and add them to Chosen’s container div.

    • max_selected_options integer

      Limits how many options the user can select. When the limit is reached, the chosen:maxselected event is triggered.

    • no_results_text string

      The text to be displayed when no matching results are found. The current search is shown at the end of the text (e.g., No results match “Bad Search”).

    • placeholder_text_multiple string

      The text to be displayed as a placeholder when no options are selected for a multiple select.

    • placeholder_text_single string

      The text to be displayed as a placeholder when no options are selected for a single select.

    • search_contains bool

      By default, Chosen’s search matches starting at the beginning of a word. Setting this option to true allows matches starting from anywhere within a word. This is especially useful for options that include a lot of special characters or phrases in ()s and []s.

    • single_backstroke_delete bool

      By default, pressing delete/backspace on multiple selects will remove a selected choice. When false, pressing delete/backspace will highlight the last choice, and a second press deselects it.

    • width int

      The width of the Chosen select box. By default, Chosen attempts to match the width of the select box you are replacing. If your select is hidden when Chosen is instantiated, you must specify a width or the select will show up with a width of 0.

    • display_disabled_options bool

      By default, Chosen includes disabled options in search results with a special styling. Setting this option to false will hide disabled results and exclude them from searches.

    • display_selected_options bool

      By default, Chosen includes selected options in search results with a special styling. Setting this option to false will hide selected results and exclude them from searches.

Note: this is for multiple selects only. In single selects, the selected result will always be displayed.

  • element Js Object

    The jQuery element object.

Examples

1. Limit the number of items which may be selected in a multi-select field.

gform.addFilter( 'gform_chosen_options', function ( options, element ) {
    //form id = 85, field id = 5
    if ( element.attr( 'id' ) == 'input_85_5' ) {
        options.max_selected_options = 2;
    }

    return options;
} );

2. Configure Chosen to use a percentage based width.

gform.addFilter( 'gform_chosen_options', function ( options, element ) {
    options.width = '75%';

    return options;
} );

3. Allow matches starting from anywhere within a word.

This can be done by enabling the search_contains option provided by the Chosen script.

gform.addFilter( 'gform_chosen_options', function ( options, element ) {
    options.search_contains = true;
    return options;
} );

Placement

JavaScript should be placed in a JS file included by your theme or a custom plugin.

Source Code

This filter is located in “gravityforms.js”