Whilst working on making a website more accessible I came across an issue regarding the 'Go back to the top' button that is available on most sites. I had to find a solution to scroll to the top and then set the keyboard focus to the top of the page so users could start tabbing and navigating again from the top.
First I added a link directly after the body (and before my accessible 'skip to content' link) that is hidden using the following code:
<body>
<a id="hidden-top-of-the-page-link" tabindex="-1"></a>
<a class="skip-to-content-link" tabindex="0" href="#main-content">Skip to content</a>
Then in my 'to top' button function I made this hidden link focus after calling my scroll event.
$("#to_top").on("click", function () {
$("html, body").animate({scrollTop: $("html").offset().top}, 500);
$("#hidden-top-of-the-page-link").focus();
});
This solution seems to work great though feels quite hacky. What I would really like to do would be to 'reset' the keyboard position on the page and was wondering if anyone knew a good way / best practice way to do that.
Thanks in advance all.
Note: Looked first on the question how to set focus on top of the page but all answers were focussed around scrolling which is not what I need help with here.