Search code examples
javascripthtmldom-eventsevent-propagation

How to disable TAB key in Mozilla Firefox?


I want to disable the tab key in my HTML form. I found following JavaScript code to disable tab, but it doesn't work in Firefox (working in Chrome and IE).

<script type="text/javascript">
document.onkeydown = function () {
    if (window.event && window.event.keyCode == 9) { // Capture and remap TAB
        window.event.keyCode = 9;
    }
    if (window.event && window.event.keyCode == 9) { // New action for TAB
        alert('The TAB key was pressed');
        return false;
    }
}    
</script>

This is my HTML form:

<body>
    <form>
        <input type='text'><br>
        <input type='text'><br>
        <input type='text'><br>
        <input type='text'><br>
        <input type='text'><br>
        <input type='text'><br>
        <input type='text'><br>
        <input type='submit'><input type='reset'>
    </form>
</body>

Solution

  • event.stopPropogation() or event.cancelBubble() (for certain version of IE) will stop an event from propagating upwards, including the default handler.

    As others have said, it's a bad idea to be preventing tab from working properly. From a user's point of view, disabling tab is likely to become very irritating.