Saturday, 28 July 2018

Move to first 'default' focus of web page

Say you have website, and when you refresh it, if you check which element has focus, using this code:

setInterval(function () {
    console.log('ID: ', $(document.activeElement).attr('id') + ' Tag: ' + $(document.activeElement).prop('tagName')
        + ' C: ' + $(document.activeElement).attr('class'));
}, 2000);

You will see that tag of element is 'BODY' element. How do you restore focus to the same element using javascript because, things like $('body').focus(); doesn't work.

EDIT: It should also reset the 'focus' flow of document. So after you click TAB it will focus the same element as if you would refresh page then click TAB.

EDIT 2: I need to reset focus on some action like keyDown, to default state - the state after you load page. From my research I know that element focused after you load page is 'body', and then after you click Tab, the first element in focus flow of your website is focused. I can't do that using $('body').focus(); - it doesn't focus body and doesn't reset current flow of focus of document.



from Move to first 'default' focus of web page

No comments:

Post a Comment