Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop default action of tab key in HTML page

In my HTML page multiple components exist and on pressing tab button, focus is set to some components (not in sequence). How do I stop tab from putting focus on any component on entire page ? Using outermost div id of html page can we block tab from putting focus on any component and on location bar as well ?

I have added jsfiddle (demo) as well where even I am not using tabindex but still focus is coming so kindly tell me how to disable tab click.

$(document).on('keydown', function(event) {
   if (event.keyCode == 9) {   //tab pressed
      event.preventDefault(); // stops its action
   }
});
like image 343
user2800089 Avatar asked Feb 13 '23 14:02

user2800089


1 Answers

I tried below code and its working fine in my scenario :

$(document).keydown(function (e) 
{
    var keycode1 = (e.keyCode ? e.keyCode : e.which);
    if (keycode1 == 0 || keycode1 == 9) {
        e.preventDefault();
        e.stopPropagation();
    }
});
like image 200
user2800089 Avatar answered Feb 15 '23 05:02

user2800089