1

I know it is possible to use JQuery.Event to simulate "Enter" keypress. Something like this does that -

var event = jQuery.Event( "keydown" );
event.keyCode = 13
$("input").trigger( event );

However, this does not fire the browser default behavior when the actual "Enter" key is pressed. For example, going to next line if the cursor is in textarea or submitting the form when the cursor is in input tag.

I want to know if it is possible to trigger or simulate the actual "Enter" keypress event/behavior.

th1rdey3
  • 4,176
  • 7
  • 30
  • 66

3 Answers3

0

Try this : Js fiddle

var e = jQuery.Event("keypress");
e.which = 13 
$("#test").keypress(function(){
   alert('keypress triggered')
}).trigger(e)
Ankit Tyagi
  • 2,381
  • 10
  • 19
-1

Do you want some thing similar this one?

 $(function(){
   $(document).keypress(function(event) {
      if(event.which == 13) {
      console.log("Dude! You hit Enter Key!!");
     }
   });
 });
Krish R
  • 22,583
  • 7
  • 50
  • 59
-1

you can use this:

$("body").delegate("input", "keypress",function(e){
  if(e.which == 13){
  }
}

it's work for all input .