3
In the example Chosen - Allow Deselect on Single Selects the possibility to uncheck the selection is implemented. But the same only works with the mouse, IE, with the keyboard has no (not discovered) how to remove the selection.
For this reason, I’m willing to implement in Chosen, a method so that when pressing the DEL key, the filled value is removed (clean, ie remove the selection without using the mouse).
So I wrote a small function that applies Chosen (settings cited in the official examples for Chosen to work), in addition, it also tests which key was pressed, and if it is the "DEL" key, I call the method limparChosen
, whose code follows below:
$('#meu_select').trigger('chosen:clear');
To trigger _this.results_reset(evt);
, that’s inside Chosen.prototype.register_observers = function () {
, I added to the file chosen.jquery.js
, the following part within Chosen.prototype.register_observers = function () {
, just below this.form_field_jq.bind("chosen:close.chosen", function(evt) {
, more or less from line 699 of the original file, the following excerpt:
this.form_field_jq.bind("chosen:clear.chosen", function (evt) {
_this.results_reset(evt);
});
To use, I test if the DEL key has been pressed, and if so, I run:
$('#meu_select').trigger('chosen:clear');
With this, everything works properly.
However, I want an alternative way to implement the above mentioned functionality without having to change the Chosen source code, because whenever I update Chosen, I will have to edit the file. What would be boring and easy to forget.
How do I implement this feature without having to change the Chosen source code?
Wouldn’t it be the case to manually make a small script for this without touching Hosen? <script> ... makes an onkey with if key = del, if it executes the method, and discards the event, otherwise If the event is handled by JS normally </script> - I can’t see it now pq tou de saida, but if you mount an http://codepen.io/pen. with Chosen, and leave the link in the comments, me or some other colleague can test something to post as an answer here, make it easier for us.
– Bacco
This has already been done, but to get the selection does not work. I could only insert the BIND mentioned above in Chosen.jquery.js
– Allan Andrade
I updated the question to make it clear.
– Allan Andrade