You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm encountering a problem while using select2. What I want is for it to find the data I type in the search box in the database, and if it can't find it, to create a new label.
However, when I use AJAX and receive a "no results found" error, I can't use the Enter key. If it finds at least one result, it creates a new label, but if it doesn't find any results, the Enter key doesn't work. Labels are only created if I have defined "tokenSeparators" and press those keys.
Enter can create a tag when an AJAX request succeeds with an empty result list. I tested this minimal setup in Chromium with the current Select2 bundle:
For a search with no matches, /search returns HTTP 200 with:
{"results":[]}
Typing new-label then pressing Enter selected a newly created option with value new-label in the test; no tokenSeparators were configured.
A successful search with zero matches is different from a failed HTTP request or an invalid response shape. If your endpoint uses another shape, processResults needs to return an object with a results array. A custom createTag callback can also suppress the proposed tag by returning null.
Could you share your Select2 initialization and the Network-panel status/JSON response for a search with no matches? This would help distinguish a response/configuration issue from a keyboard bug. The test above creates the selected option in the browser; saving that new label to your database is a separate application step.
Activity
sridharkalaibala commentedon Sep 13, 2026
Enter can create a tag when an AJAX request succeeds with an empty result list. I tested this minimal setup in Chromium with the current Select2 bundle:
For a search with no matches,
/searchreturns HTTP 200 with:{"results":[]}Typing
new-labelthen pressing Enter selected a newly created option with valuenew-labelin the test; notokenSeparatorswere configured.A successful search with zero matches is different from a failed HTTP request or an invalid response shape. If your endpoint uses another shape,
processResultsneeds to return an object with aresultsarray. A customcreateTagcallback can also suppress the proposed tag by returning null.Could you share your Select2 initialization and the Network-panel status/JSON response for a search with no matches? This would help distinguish a response/configuration issue from a keyboard bug. The test above creates the selected option in the browser; saving that new label to your database is a separate application step.