Chosen: A way to get user typed text on "chosen:no_results"

Created on 16 Jul 2015  路  9Comments  路  Source: harvesthq/chosen

If you define a handler for 'chosen:no_results' event its pretty tricky to get the text (what provided the 'no result') from user input.

Could you please add a function (or extra parameter to the callback) to make it possible to get the text from the input field?

Most helpful comment

var obs = $('select').chosen({width: '100%'}).data('chosen');
            obs.search_field.on('keyup', function(e){
                console.log($(this).val());
            });

All 9 comments

+1

Seconded. It seems that the current method doesn't actually pass the terms through, though I was able to grab them in a super awkward way:

$('.select--chosen').on('chosen:no_results', function(event, parameters) {
    console.log(parameters.chosen.search_results[0].textContent.match(/No results match "(.+)"/)[1])
  });

Has anyone any idea how to solve that problem?

@dzeju555 which problem? The code I posted above makes it possible to grab the text the user typed, it's just a bit awkward. Otherwise, a pull request needs to make changes to the library to give us an easier way to get at the data.

I mean, the easier way to do that. I have to do a selectlist which allows user to select an option. But if there is no option user typed, I have to add it to a database dictionary. I though that it will be a good option that Chosen could sent typed text in form with another input fields to my controller (I use MVC application) and there I would do the rest. What is important, I want typed text to be still in Chosen field, now it disappears when Chosen is closed and user is not sure what will be sent to the database. I will try to replace placeholder with typed text in jQuery when not found trigger is ran.

var obs = $('select').chosen({width: '100%'}).data('chosen');
            obs.search_field.on('keyup', function(e){
                console.log($(this).val());
            });

+1

Maybe this can works.

$('.select-chosen').on('chosen:no_results', function(event, element){ console.log($(element.chosen.search_results).find('span').text()); });

This is already possible:

$('.chosen-select').on('chosen:no_results', function(event, data){
  console.log(data.chosen.get_search_text());
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

zerocrates picture zerocrates  路  7Comments

jim-at-miramontes picture jim-at-miramontes  路  4Comments

jbrooksuk picture jbrooksuk  路  6Comments

vpode picture vpode  路  5Comments

Scalamoosh picture Scalamoosh  路  8Comments