Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind keypress event in jquery autocomplete?

As you can see in http://jsfiddle.net/hn838/4/ 'click' is working fine for autofiller, But I want to trigger same code on enter key keypress also, but it is not working for me. My code is here for both. Click :

$('.ui-autocomplete').on('click', '.ui-menu-item', function(){
    $('.college').trigger('click');
});

Keyprss :

 $('.ui-autocomplete').on('keypress', '.ui-menu-item', function(e){
            if (e.which == 13) {
                         e.preventDefault();
            $('.college').trigger('click');
           }
        });

I want $('.college').trigger('click'); execution on click and enter keypress. click is working fine but keypress in not working.any clue ?

like image 290
Anshul Avatar asked Feb 16 '23 16:02

Anshul


1 Answers

Rather than binding to key events, you could add a function to the select event:

select: function(){
    $('.college').trigger('click');
}

That way it'd only trigger when the user actually selected the autocomplete value.

http://jsfiddle.net/louisbros/hn838/10/

like image 102
louisbros Avatar answered Feb 19 '23 20:02

louisbros