javascript – How to make Chosen ignore accents in the search?

Question:

I'm using Chosen in my forms, but I'd like it to ignore accents when filtering.

For example: In a city listing, correctly filter it to:

– Africa

But it doesn't filter to (as the words differ by accent):

– Africa

On the project's GitHub, there have been discussions about this since 12/03/2012 ( https://github.com/harvethq/chosen/issues/536 ), but they haven't implemented it in the official code yet.

Reading the existing discussions, I found this working example, posted by @felpasl :

However, I intend to implement its operation, but without changing the original source codes, to minimize future complications when updating the code from the official repository.

I thought about replacing the method, externally, but I'm not able to do it. Below is the code I tried to replace the method…

var Chosen = $('#campo_select_com_chosen').chosen();
Chosen.prototype.search_string_match = function (search_string, regex) {
    alert('Método substituído com sucesso');
};

But I get this error:

Uncaught TypeError: Cannot set property 'search_string_match' of undefined

My difficulty is how to replace a method of the lib I'm using without changing the sources of the same. If it was in PHP I would inherit the class and override the method… but in JavaScript I don't know how to do it.

Could someone help me on how to make this implementation, like PlugIn, in Chosen?

Answer:

On StackOverflow in English there was a similar question, and there is an issue open on GitHub requesting this change.

In this issue there are several users putting ways to change the code, one of them posted in this Stack's answer and should already solve your problem.

It's in JSFiddle: http://fiddle.jshell.net/whqb5/1/
It should work by importing right under Chosen because of prototypes.

Or if you want a more straightforward solution, in the latest responses in Issue the user Hanoii created a version of Chosen that is capable of doing this type of research: https://github.com/hanoii/chosen/releases

Question : https://stackoverflow.com/questions/31960166/chosen-search-accented-words
Issue : https://github.com/harvethq/chosen/issues/536

Scroll to Top