Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Enter key is pressed with jquery

I have a text field which is showing a value. What I want is when the user writes a new value in that text field and presses enter, an ajax function is triggered to do a pagination operation. I have a text field like this:

<input type="text" id="page" name="page" value="<?php echo($this->pn);?> /> 

And when a user writes any new value and presses Enter, I want the following ajax function triggered:

 update_ajax2({rpp:<?php echo($this->rpp);?>,pn:document.page.paged.value,filter:'<?php echo($this->filter);?>',orderby:'<?php echo($this->orderby);?>'}); 

I tried using the keypress event to detect if(e.which===13) but this doesn't solve the problem. Can anyone guide me?

like image 913
Awais Qarni Avatar asked Apr 12 '11 07:04

Awais Qarni


People also ask

How do you detect if Enter is pressed?

Check the event.The keyCode property returns a number for the key that's pressed instead of a string with the key name. When it returns 13, then we know the enter key is pressed.

How do you trigger click event on pressing Enter key?

To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery. keyup(): This event occurs when a keyboard key is released. The method either triggers the keyup event, or to run a function when a keyup event occurs.

What is Keyup and Keydown in jQuery?

jQuery keyup() Method The order of events related to the keyup event: keydown - The key is on its way down. keypress - The key is pressed down. keyup - The key is released.

What is keypress event in jQuery?

The keypress() method triggers the keypress event, or attaches a function to run when a keypress event occurs. The keypress event is similar to the keydown event. The event occurs when a button is pressed down. However, the keypress event is not fired for all keys (e.g. ALT, CTRL, SHIFT, ESC).


1 Answers

<input type="text" id="txt"/>  $('#txt').keydown(function (e){     if(e.keyCode == 13){         alert('you pressed enter ^_^');     } }) 

On jsfiddle.

like image 76
Santosh Linkha Avatar answered Oct 11 '22 07:10

Santosh Linkha