Suppose I've got the Code structure below. If I focus an element within my red container (one of the inputs), the focusin
event triggers. Likewise, if I click outside the red container, the focusout
event triggers.
However, if I click one of the input elements, then directly the other, both a focusout
and a focusin
event get triggered in quick succession.
Is there an easy way to avoid this or find out whether the second focusout
event can be ignored because focus in fact stays within the relevant element, aside from ugly solutions like setting a flag on the first focusout
event and waiting for a render tick to see whether another focusin
event happens?
document.getElementById("el").addEventListener("focusin",
() => document.getElementById("out").innerHTML += "focusin<br/>");
document.getElementById("el").addEventListener("focusout",
() => document.getElementById("out").innerHTML += "focusout<br/>");
<div id="el" style="background-color: red; padding: 4px">
<input />
<input />
</div>
<div id="out">
</div>