2

I got the code from here jquery how to catch enter key and change event to tab

(function($) {
    $.fn.enterAsTab = function(options) {
        var settings = $.extend({
            'allowSubmit': false
        }, options);
        this.find('input, select, textarea, button').live("keypress", {localSettings: settings}, function(event) {
            if (settings.allowSubmit) {
                var type = $(this).attr("type");
                if (type == "submit") {
                    return true;
                }
            }
            if (event.keyCode == 13) {
                var inputs = $(this).parents("form").eq(0).find(":input:visible:not(disabled):not([readonly])");
                var idx = inputs.index(this);
                if (idx == inputs.length - 1) {
                    idx = -1;
                } else {
                    inputs[idx + 1].focus(); // handles submit buttons
                }
                try {
                    inputs[idx + 1].select();
                }
                catch (err) {
                    // handle objects not offering select
                }
                return false;
            }
        });
        return this;
    };
})(jQuery);

My questions are:

  1. I want this script also work on select (dropdown), textarea, button[not with type=submit]. It is working great on textarea, and buttons[not with type=submit] but it is not working with select(dropdown).

  2. I found out that this script failed to move to next input when passing a disabled input. For example I have three input, qty, qtyConv and memo. all of them are textfields, but qtyConv is disabled. When I am in qty and I press enter, I could not move to memo.

Any help to improve this script to meet my requirements?

Thank you in advance.

Daniel

Solution: use keydown instead of keypress as suggested by Nitish Dhar.

Community
  • 1
  • 1
Daniel Adinugroho
  • 1,399
  • 2
  • 11
  • 21
  • Post this in a **[fiddle](http://jsfiddle.net/)**. And post your html markup. – Shaunak D May 27 '14 at 11:03
  • I am not an expert, apologise for mistakes. I am not quite understand how jsfiddle works, since my project is a bit complex and written in yii. I don't know how to translate to jsfiddle. Hence, I create a simple scenarioin here http://jsfiddle.net/3g3r6/. But, I am sure that my yii is correct if I am having all textfields and no disabled on any textfield, it is working as expected – Daniel Adinugroho May 27 '14 at 11:28
  • Welcome to SO! Instead of adding "SOLVED" to the question title, please mark the answer that helped you as "Accepted" (green tick below the score). See [FAQ: What does it mean when an answer is "accepted"?](http://stackoverflow.com/help/accepted-answer) – Kos May 29 '14 at 08:23
  • To follow on from what @Kos has said, if the existing answers do not answer the question, you can always answer it yourself and mark that as the accepted answer. – James Donnelly May 29 '14 at 08:28

1 Answers1

9

Couple of things -

  1. Selector being used is wrong. The selector defined is wrong in terms of looking for the not disabled ones, use this one instead -

    $(this).parents("form").eq(0).find(":input:visible:not(:disabled):not([readonly])"); 
    
  2. Firefox 29.0 has a bug with keypress on select inputs, use keydown instead - Select is not working for you in firefox due to a bug (or not a bug) where they don't listen to keypress when its on a select input - https://support.mozilla.org/en-US/questions/998291.

WORKING DEMO - http://codepen.io/nitishdhar/pen/Gxbhm (works in chrome & FF as well)

Complete Code

(function($) {
    $.fn.enterAsTab = function(options) {
        var settings = $.extend({
            'allowSubmit': false
        }, options);
        $(this).find('input, select, textarea, button').live("keydown", {localSettings: settings}, function(event) {
            if (settings.allowSubmit) {
                var type = $(this).attr("type");
                if (type == "submit") {
                    return true;
                }
            }
            if (event.keyCode == 13) {
                var inputs = $(this).parents("form").eq(0).find(":input:visible:not(:disabled):not([readonly])");
                var idx = inputs.index(this);
                if (idx == inputs.length - 1) {
                    idx = -1;
                } else {
                    inputs[idx + 1].focus(); // handles submit buttons
                }
                try {
                    inputs[idx + 1].select();
                }
                catch (err) {
                    // handle objects not offering select
                }
                return false;
            }
        });
        return this;
    };
})(jQuery);

$("#form").enterAsTab({ 'allowSubmit': true});
Nitish Dhar
  • 2,282
  • 15
  • 18