Skip to content

Can’t fully reset activeElement (focus for the Tab key) to its default position

OK, I’m trying to reset the activeElement from the middle of the page so that the tab key would start from the top like, the same way as the page is just refreshed.

For that purpose (tested in FF and Chrome) I’m trying to use document.activeElement.blur() (from the browser console). As result, the selection of the <a href></a> gets visually removed (nice). Also,running document.activeElement after running document.activeElement.blur() from console shows

<body class="ng-tns-0-0">

which looks good (the activeElement is body now?)

However, if I close the console and hit the Tab key, the focus appears on the next to the previous a href – Not to the link that is focused on page load + Tab key.

Why and how to fix that behavior?

The question appeared from the accessibility point of view, as the significant part of the page gets rendered with another content. The tab key needed to start over, like for a new page.

Answer

In fact, you shouldn’t use blur() ever, and this method shouldn’t even exist.

  • After having called blur(), you have no control of where the focus goes. It may go in menu bar, toolbars, or even go totally outside of the browser and/or become completely unrecoverable without a mouse.
  • The behavior you observe with firefox and chrome isn’t standard, isn’t specified anywhere, may depend on OS and/or browser settings, and you don’t have control at all on it

The safest solution if you want to go back to the first element of the page is probably to focus that first element, rather than calling blur() and hope for the best.

In order for any application or website to be keyboard accessible, the focus must always be under control, i.e. you must always know exactly where it is. As the method blur() doesn’t specify where the focus goes next, you lose control of the focus when using it; so you should never use it. As far as I know, it has probably no legitimate use.