Changing search behavior in jquery plugin Chosen

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

search_contains: true

Options documentation


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

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

The method that makes the search in the plugin is Chosen.prototype.winnow_results. It uses a regular expression that matches text that “starts with” the 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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)