views:

125

answers:

2

I have a simple search page with a single input box.

I want to be able to trigger the search action either by clicking "Go" or by pressing Enter in the input box. I did it like this:

$("input[name='entry']").keyup(function(event) {
                                 if (event.keyCode == 13) {
                                     search_phone();
                                 }
                             });

 $('a#go').click(function() {
              search_phone();
 });

Is there a more elegant way to do this? Like with bind and trigger, or fling. If so, how?

+6  A: 

Not much can you improve here. Your code is pretty good.

You could skip the anonymous function for the click event.

$('a#go').click(search_phone);
RaYell
A: 

I would just make your "go" link the submit button

<input type="submit" name="submit" value="go"/>

And then just bind your function to the submit (which would happen either from pressing enter while in the text box or by clicking the go button.

$('#my_form').submit(search_phone);
idrumgood