Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

capturing enter key in select2

I'm using select2 to present an editable selectbox. When user writes a statement which does not appear in the list(select2, data), I show a button to add this statement to the list.

Forcing users to click the button seems to me a little bit frustration. Is it possible to capture enter key in select2? I want to make user able to add his/her new statements into the list just by pressing enter key.

like image 660
Muatik Avatar asked Dec 26 '13 08:12

Muatik


3 Answers

$('select2-search-field > input.select2-input').on('keyup', function(e) {
   if(e.keyCode === 13) 
      addToList($(this).val());
});
like image 161
Vishal Avatar answered Nov 08 '22 05:11

Vishal


I'm using Select2 4.0.3 and this works form me:

$(document).on('keyup', '.select2-search__field', function (e) {
    if (e.which === 13) {
        alert('Pressed enter!');
    }
});
like image 37
aleixfabra Avatar answered Nov 08 '22 07:11

aleixfabra


I am using Select2 4.0. This works for me;

$('.select2-search__field').on('keyup', function (e) {
        if (e.keyCode === 13)
        {
            alert('Enter key');
        }
    });
like image 3
Baqer Naqvi Avatar answered Nov 08 '22 05:11

Baqer Naqvi