jruiz jruiz - 29 days ago 8
Javascript Question

I can't capture enter key in select2

I am using select2, and I need to capture the enter key, but I can't.

I used:

$(document).on('keydown', '.select2-input', function (ev) {

if (ev.which == 13) {
alert('press enter')
}

if (ev.which == 9) {
ev.preventDefault();
ev.stopImmediatePropagation();
alert('press tab')
}
});


I can capture all the keys but for the enter.

Can someone help me?

Answer Source

Try this out:

$('select2-search-field > input.select2-input').on('keyup', function(e) {
   if(e.keyCode === 13) 
      alert('enter key event');
});