views:

621

answers:

2

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?

+3  A: 

Here is a solution.

Sarfraz
That solution doesn't look so great to me. It pays no attention to "tabindex" properties, for one thing. Also, it's specific to forms and input elements, while (for reasons unknown) the original asker apparently wants the effect to be global across the entire page.
Pointy
@Pointy: this is not the eternal solution, would love to see other possibilities from you or any one else :)
Sarfraz
looks like a solution to me =)
mkoryak
It does the trick, but I'd like to see if any other people have different solutions.
payling
@payling: sure, that's better decision :)
Sarfraz
A: 
$('input').live("keypress", function(e) {
            /* ENTER PRESSED*/
            if (e.keyCode == 13) {
                /* FOCUS ELEMENT */
                var inputs = $(this).parents("form").eq(0).find(":input:visible");
                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;
            }
        });

visible input cann't be focused.

ldp615