In javascript, how to eliminate any focus in the document?

BugsForBreakfast

I mean, is there a way to force a blur? so if any component (button, inputext, etc.) is focused it will be blured, hope this makes sense :)

like a document.blur(), I need this because everytime user pressed F5 I need to eliminate any focus before it refreshes.

T.J. Crowder

You can use document.activeElement.blur() but in my experience, some element is always going to have focus, even after you do that. For instance, on Chrome, the following ends up with body having focus, even after removing focus from (say) the input:

setInterval(() => {
    if (document.activeElement) {
        document.activeElement.blur();
    }
    console.log(document.activeElement && document.activeElement.nodeName);
}, 1000);
<input type="text" id="x">

But it does remove focus from the input, perhaps that's good enough for your use case.


From your comments explaining the situation further, I'd tend to think you'd want:

if (document.activeElement && document.activeElement !== document.body) {
    document.activeElement.blur();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you clear the focus in javascript?

How to check if any JavaScript event listeners/handlers attached to an element/document?

How to set focus on first form element in any bootstrap modal

How to eliminate hover, active, focus gray color in Material UI Chip component

How to check if any form element inside a fieldset has focus

How to focus next input (if any) on next press

How to focus on row using input field in JavaScript?

set focus to any node in large svg document

How to focus on a range slider using Javascript?

How to maintain focus after click in JavaScript?

How to clear text input and re focus in JavaScript

How to set focus with javaScript?

How to prevent a document-based cocoa application to open ANY document?

How focus on the document after find?

JavaScript how to use Document innerText?

Javascript Focus using .focus()

how to eliminate any number after the floating point?

Photoshop Script - How to check if any document is open?

AngularJS2: How to focus input like document.getElementById(..).focus in javascript?

Strange Output in Expect Recordings: How to eliminate, and any reasons why?

Override javascript to eliminate flickering

How to set focus on list element in Javascript?

How to eliminate all paths that pass through particular document or vertex while during Graph Traversal in ArangoDB

How to change the focus of an HTML input element in Javascript

how to access the css on focus in javascript?

How to select document body in Javascript

how to eliminate duplicates but keep the document with more fields in MongoDB

How do I focus Word document after using userform in VBA?

how to replace group-focus with JavaScript