thermans
thermans

Reputation: 1167

Jquery: one call triggered by multiple events

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?

Upvotes: 2

Views: 506

Answers (2)

idrumgood
idrumgood

Reputation: 4924

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);

Upvotes: 0

RaYell
RaYell

Reputation: 70414

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);

Upvotes: 6

Related Questions