Is there any way in JavaScript to focus the document (content area)?

0

Issue

Is there any way to set focus to the document, i.e. the content area, in JavaScript? document.focus() doesn’t seem to do anything.

Solution

In HTML 4.01, focus is only discussed in the context of elements such as form controls and links. In HTML 5, it is discussed much more widely. However, how focus works for documents is mostly browser dependent.

You might try:

// Give the document focus
window.focus();

// Remove focus from any focused element
if (document.activeElement) {
    document.activeElement.blur();
}

It is very well supported as well.

Answered By – RobG

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More