I have a select2 dropdown for countries (multiselect). When user types keywords, it shows the related items in the menu.
For e.g., if user types ind, the menu shows India and Indonesia.
If the enter key is pressed, the first item (India) is selected. This is the default behavior.
Now, I would like to select both the countries when user types ind and presses enter.
$(".select2-search__field").on("keyup", function (e) {
console.log(e.keyCode);
if (e.keyCode == 13) {
alert();
}
});
With the above code, the console logs show up for i, n & d but not for the Enter key, and hence the alert never shows.
I guess the default select2 behavior is preventing my code to be triggered.
What should I do, so that I can capture the enter key press.