Quote Originally Posted by paul_wilkins
No, not like that. That will prevent the default of every key that is ever pressed.
I thought so, but I wanted to try that to confirm it with you.

Quote Originally Posted by paul_wilkins
Secondly, you may want to call a function when the tab key is pressed, so that you can more easily encapsulate what you want to achieve.
Also why have a function handleTabKey() return something. Isn't it useless or less useful right now, but wouldn't it help later in executing other things after interrupting the tab's default function ?


But for now, without other things happening, can't we set allowDefault to false without a function like so:
Code JavaScript:
body.onkeydown = function (evt) {
    var allowDefault = true;
    evt = evt || event;
 
    if (evt.keyCode === 9) {
        // tab was pressed
        allowDefault = false
    }
    return allowDefault;
 
    if(allowDefault === false){
        alert("tab was prevented and replaced with this alert");
    }  
};

or this... (it seems like they do the same thing— the latter with less code)

Code JavaScript:
body.onkeydown = function (evt) {
    var allowDefault = true;
    evt = evt || event;
 
    if (evt.keyCode === 9) {
        // tab was pressed
        alert("tab was prevented and replaced with this alert");
    }
    return allowDefault;
};

or even this...
(or without using allowDefault at all, for now, and therefore not intialising or returning it. And executing what replaces the tab's default behaviour straight when the tab key is detected in the if statement*)
Code JavaScript:
body.onkeydown = function (evt) {
    evt = evt || event;
 
    if (evt.keyCode === 9) {
        // tab was pressed
        alert("tab was prevented and replaced with this alert");
    }
};
*I can see how this would be messy code, is that why you suggested to encapsulate it into a function?

2 more questions:
1.) So do all of the above do the same thing?
2.) Is that same thing interrupting the tab button's function once pressed and alerting the message instead?