I have a form where whenever one of the fields is focused (i.e., the user has clicked or tabbed into it), a div with extra information about that field is shown. Pretty simple: onFocus
sets display: none;
on this info pane, and onBlur
removes it.
I only want these events to fire when clicking other elements on the same page, but they also fire when switching to another window or tab. Super annoying to see content come and go on the page every time you <Alt>-<Tab>
.
Is there any way for JS to distinguish between these two kinds of blur events?
EDIT: I made a codepen to illustrate the problem. Open it up, click on the text input field, then alt-tab to another window to see some of the text disappear.
Here is the code in question:
<input id="foo" />
<p>
Lorem ipsum dolor <span id="bar">sit amet consectetur</span>
</p>
.hidden {
display: none;
}
const inputField = document.getElementById('foo')
const hiddenSpan = document.getElementById('bar')
inputField.addEventListener('focus', () => hiddenSpan.classList.add('hidden'))
inputField.addEventListener('blur', () => hiddenSpan.classList.remove('hidden'))