Thursday 6 June 2013

Enter key press behaves like a Tab in Javascript

Apply this code in .aspx file when fill form on enter

$('input , select , textarea').live("keypress", function (e) {
                if (e.keyCode == 13) {
                    var inputs = $(this).parents("form").eq(0).find(":input", "select", ":textarea");
                    var idx = inputs.index(this);
                    if (idx == inputs.length - 1) {
                        inputs[0].select()
                    } else {
                        if (inputs[idx + 1].readOnly == true) {
                            inputs[idx + 2].focus(); // handles submit buttons
                            inputs[idx + 2].select();
                            return false;
                        }
                        inputs[idx + 1].focus(); // handles submit buttons
                        inputs[idx + 1].select();
                    }
                    return false;
                }
            });

No comments:

Post a Comment