Detect Enter key is pressed with jquery

AjaxJqueryJavascript Events

Ajax Problem Overview


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?

Ajax Solutions


Solution 1 - Ajax

<input type="text" id="txt"/>

$('#txt').keydown(function (e){
    if(e.keyCode == 13){
        alert('you pressed enter ^_^');
    }
})

On jsfiddle.

Solution 2 - Ajax

Use this, it works for me :)

$("#search-txt" ).on( "keydown", function(event) {
	if(event.which == 13) 
		return false;//do something
});

Solution 3 - Ajax

Solution 4 - Ajax

Key pressed event should work try it

function keyPressedTest(a)
{
	a=(a)?a:window.event;
	input=(a.target)?a.target:a.srcElement;
	if(a.keyCode==13)
	{
		your_function()
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAwais QarniView Question on Stackoverflow
Solution 1 - AjaxSantosh LinkhaView Answer on Stackoverflow
Solution 2 - AjaxvcgView Answer on Stackoverflow
Solution 3 - AjaxentropoView Answer on Stackoverflow
Solution 4 - AjaxMuhammad UmmarView Answer on Stackoverflow