Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert An Enter Key Press Into A Tab Key Press For Web Pages

The enter key press should work like a Tab key press.The enter key press for TextArea and Submit Button should work as usual.Focus should skip from the next element when the next field is disabled/readonly.

thanks,

like image 753
deep Avatar asked Dec 29 '11 05:12

deep


People also ask

How do I change the enter key on a tab?

Add("onKeyDown", "ModifyEnterKeyPressAsTab();"); In the above example, any time a user has focus on the Textbox and presses a key our JavaScript function will be called. If the key press is Enter, the function will return a Tab key press which moves focus to the next control in TabIndex order.

What is e keyCode === 13?

Keycode 13 is the Enter key.


2 Answers

First off, this is probably not a great idea usability-wise. However, here's something that should work:

$(":input").on("keydown", function(event) {
    if (event.which === 13 && !$(this).is("textarea, :button, :submit")) {
        event.stopPropagation();
        event.preventDefault();

        $(this)
            .nextAll(":input:not(:disabled, [readonly='readonly'])")
            .first()
            .focus();
    }
});

Example: http://jsfiddle.net/NDcrk/

The piece that finds the next input may have to change, depending on your markup.

like image 59
Andrew Whitaker Avatar answered Oct 23 '22 02:10

Andrew Whitaker


This solution work for me. Tested it on IE and FireFox.

<script type="text/javascript">
    function tabE(obj, e) {
        var e = (typeof event != 'undefined') ? window.event : e; // IE : Moz 
        if (e.keyCode == 13) {
            var ele = document.forms[0].elements;
            for (var i = 0; i < ele.length; i++) {
                var q = (i == ele.length - 1) ? 0 : i + 1; // if last element : if any other 
                if (obj == ele[i]) {
                    ele[q].focus();
                    break
                }
            }
            return false;
        }
    }
</script>

<form METHOD="POST">
    <input name="" type="text" onkeypress="return tabE(this,event)">
    <br>
    <input name="" type="text" onkeypress="return tabE(this,event)">
    <br>
    <input name="" type="text" onkeypress="return tabE(this,event)">
    <br>
    <INPUT TYPE="submit" Value="Ok">
</form>

I found it here.

like image 27
Ajit Poudel Avatar answered Oct 23 '22 03:10

Ajit Poudel