bookmark_bordergform_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

<script type="text/javascript">
    gform.addFilter('gform_chosen_options', 'set_chosen_options_js');
    function set_chosen_options_js(options, element){
        return options;
    }
</script>

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

Example One

This example uses the gform_pre_render filter to load the hook. This example limits the number of items which may be selected in a multi-select field.

<?php
add_action("gform_pre_render", "set_chosen_options");
function set_chosen_options($form){
    ?>

    <script type="text/javascript">
        gform.addFilter('gform_chosen_options','set_chosen_options_js');
        //limit how many options may be chosen in a multi-select to 2
        function set_chosen_options_js(options, element){
            //form id = 85, field id = 5
            if (element.attr('id') == 'input_85_5'){
                options.max_selected_options = 2;
            }

            return options;
        }
    </script>

    <?php
    //return the form object from the php hook
    return $form;
}
?>

Example Two

This example shows how you can configure chosen to use a percentage based width.

<script type='text/javascript'>
gform.addFilter('gform_chosen_options', function (options, element) {
    options.width = '75%';

    return options;
});
</script>

Placement

This hook should be placed between script tags in an HTML field on your form or you may use the gform_pre_render hook to echo the script block to the page.

Source Code

This filter is located in “gravityforms.js”