4

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 ?

2
  • Since there's some confusion: Can you, please, tell us wether you really want to handle the actual keypress or the selection of an element from the list, which makes more sense. Commented Apr 27, 2013 at 10:36
  • selection of an element from the list. Commented Apr 27, 2013 at 10:39

2 Answers 2

6

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Should be the accepted answer. Using select: function(e,item){...} lets OP do exactly what he wants.
2

There is no class ui-autocomplete inside your fiddle. So I assume it looks like this:

<input type = 'text' class = 'search ui-autocomplete' />

and also there is no class ui-menu-item in your fiddle, so you should do like this:

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

Updated Fiddle


If you want to use the class that automatically generated from jQuery UI, you can do:

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

Fiddle

6 Comments

@pandit Can you check again? it's ui-autocomplete-input not ui-autocomplete
you removed '.ui-menu-item' how can i track in this code that which <li> ui-menu-item is pressed ?
@pandit Sorry but where is your .ui-menu-item element? You target it in on function but I cannot find where's this element in your fiddle.
your second fiddle is exactly match with my requirements. thanks for your effort and time..
anyway when you enter something in input field than a ul is created with suggestions in li's which have class .ui-menu-item, I am refering that...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.