Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to disable 'tab' on a input type='text'?

is there a cross-browser solution to disable 'tab' on a input type='text' ?

<input type='text' />

Pressing 'tab' moves you to the next 'focusable' component (for example a button)

I want instead 'tab' to do something else. For example Even in a google search tab is just used to autocomplete the suggested text instead of moving you to the next focusable component...

thanks

like image 894
Zo72 Avatar asked Mar 21 '13 16:03

Zo72


2 Answers

document.querySelector('input').addEventListener('keydown', function (e) {
    if (e.which == 9) {
        e.preventDefault();
    }
});

Depends on how cross-browser you are talking though, as the above only supports IE9 (and other modern browsers).

http://jsfiddle.net/ExplosionPIlls/YVUzz/

IE8- use attachEvent instead of addEventListener and e.keyCode instead of e.which, and there is no preventDefault, but you can use return false.

like image 196
Explosion Pills Avatar answered Oct 24 '22 15:10

Explosion Pills


a non jQuery implementation would be a bit like this -

HTML would be

<input type="text" id="myField1" onkeydown="return stopTab(event);" />

and JS something like below

function stopTab( e ) {
    var evt = e || window.event
    if ( evt.keyCode === 9 ) {
        return false
    }
}
like image 30
Mark Walters Avatar answered Oct 24 '22 15:10

Mark Walters