Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery how to catch enter key and change event to tab

I want a jquery solution, I must be close, what needs to be done?

$('html').bind('keypress', function(e) {      if(e.keyCode == 13)      {          return e.keyCode = 9; //set event key to tab      } }); 

I can return false and it prevents the enter key from being pressed, I thought I could just change the keyCode to 9 to make it tab but it doesn't appear to work. I've got to be close, what's going on?

like image 508
payling Avatar asked Feb 25 '10 16:02

payling


People also ask

How do you make the Enter key work like a tab?

Map [Enter] key to work like the [Tab] key This caputures both Enter and Shift + Enter .

How do you check Enter key is pressed in jQuery?

The “enter” key is represent by code “13”, check this ASCII charts. To check if an “enter” key is pressed inside a textbox, just bind the keypress() to the textbox. $('#textbox').

Why is my enter key tabbing?

If the field you've selected has a dropdown list then CLICKING ON IT ONCE will bring the list down but not place the cursor in the field. Selecting the field a second time will place the cursor in the field. Once the cursor is actually visible in the field you will find that the TAB key works as you expect.


2 Answers

Here is a solution :

$('input').on("keypress", function(e) {             /* ENTER PRESSED*/             if (e.keyCode == 13) {                 /* FOCUS ELEMENT */                 var inputs = $(this).parents("form").eq(0).find(":input");                 var idx = inputs.index(this);                  if (idx == inputs.length - 1) {                     inputs[0].select()                 } else {                     inputs[idx + 1].focus(); //  handles submit buttons                     inputs[idx + 1].select();                 }                 return false;             }         }); 
like image 54
Sarfraz Avatar answered Sep 28 '22 10:09

Sarfraz


This works perfect!

 $('input').keydown( function(e) {         var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;         if(key == 13) {             e.preventDefault();             var inputs = $(this).closest('form').find(':input:visible');             inputs.eq( inputs.index(this)+ 1 ).focus();         }     }); 
like image 31
Bharat Avatar answered Sep 28 '22 10:09

Bharat