Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery KeyUp and Click

I was wondering how you can do .keyup() and .click() for the same #id?

i.e. essentially I want to validate the #id when both the user attempts to hit enter or hits the #search button.

Thanks alot

like image 634
Tom Avatar asked May 03 '11 14:05

Tom


People also ask

What is Keyup function in jQuery?

The keyup() is an inbuilt method in jQuery which is used to trigger the keyup event whenever User releases a key from the keyboard. So, Using keyup() method we can detect if any key is released from the keyboard. Syntax: $(selector).keyup(function) Here selector is the selected element.

Why is Keyup not working?

keyup / keydown seem to only work on elements that are present at document. ready . If you are triggering your event from elements that were altered or injected post pageload, these events will not fire.

What is Keyup event?

The keyup event is fired when a key is released. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup , but as 97 by keypress .


2 Answers

$('#foo').bind('click keyup', function(event) {
  ...

You'll have to add some logic, as the event type changes, but it should work with enough if blocks.

like image 168
Blender Avatar answered Sep 21 '22 05:09

Blender


Well you can do:

        $(document).keydown(function(objEvent) {
            if (objEvent.keyCode == 13) {  //clicked enter
                 $('#search').click(); //do click
            }
        })

        $("#search").click(function(e){/*click fn*/})

Will run the click on enter press

like image 34
Naftali Avatar answered Sep 21 '22 05:09

Naftali