[ACCEPTED]-Changing search behavior in jquery plugin Chosen-jquery-plugins

Accepted answer
Score: 80

As mentionned in some more recent answers, the 9 plugin now implemements an option to change 8 the search behavior:

search_contains: true

Options documentation


The plugin does not 7 provide an option to change the search method 6 behavior.

If you're willing to change the 5 plugin source itself, here's a way to do 4 it.

The method that makes the search in the 3 plugin is Chosen.prototype.winnow_results. It uses a regular expression 2 that matches text that "starts with" the 1 search term:

// "^": means "starts with"
// "searchText" is the text in the search input (it is just cleaned up don't be scared)
regex = new RegExp('^' + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');

Change it to:

regex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');

DEMO

Score: 45

The search behavior can be set with the 3 option search_contains

This is by default false

Set it to true, and 2 chosen will also find matches inside instead 1 of only the beginning:

$('#my_dropdown').chosen({ search_contains: true });
Score: 7

As in Chosen 1.0, just add option {search_contains: true}

$('.selector').chosen({search_contains: true});

Have 1 fun.

Score: 0

in chosen 1.0 i did on line 301&302

escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
regexAnchor = "";

0

Score: 0

There is option search_contains available to search sub-string 1 in options and can be used as:

$(".chosen-select").chosen({
    search_contains: true
});

More Related questions