code-tutorial.com

The best resources for developers

How do I capture TAB key in text box?

<body>
<input type="text" id="myInput">
<script type="text/javascript">
    var myInput = document.getElementById("myInput");
    if(myInput.addEventListener ) {
        myInput.addEventListener("keydown",this.keyHandler,false);
    } else if(myInput.attachEvent ) {
        myInput.attachEvent("onkeydown",this.keyHandler); /* damn IE hack */
    }

    function keyHandler(e) {
        var TABKEY = 9;
        if(e.keyCode == TABKEY) {
            this.value += "    ";
            if(e.preventDefault) {
                e.preventDefault();
            }
            return false;
        }
    }
</script>
</body>

Tags: javascript user-interface

Source: By Seibar as answer to the question

This code snippet was collected from stackoverflow, and is licensed under CC BY-SA 4.0


Related code-snippets: