If I've a simple event handler that's called when an input
loses focus. Is there any way to tell from that event object what it lost focus to? ie, what the next focused element is?
I'm basically trying to have the search input perform one behaviour if it loses focus, but not if it loses focus to the search button that is next to the search input (as that's handled by a different listener).
So:
- submit form / click search btn =>
behaviour a
- click out of the input =>
behaviour b
- click out of the input onto the search btn =>
behaviour a
The code below would be where I would try and distinguish event 2 from 3
handleFocusEvent(e) {
const searchContainer = document.querySelector('.summary__item');
const searchInput = e.target.closest('.summary__search-input');
const nextFocusedElement = // Some property of the event object
// So I'd like this to run if the input loses focus, but the nextFocusedElement is not a specific element
if(searchInput && !nextFocusedElement) {
searchInput.value = "";
searchInput.animation.reverse();
}
}
*Edit Using 'onfocus' and 'relatedTarget' was recommended here for the problem I was getting when trying a suggested solution of activeTarget
. But now instead of getting the body
element, I'm just getting null