I need to get the newly focussed element (if any) while executing an onBlur handler.
How can I do this?
I can think of some awful solutions, but nothing which doesn't involve setTimeout.
To detect if the element has the focus in JavaScript, you can use the read-only property activeElement of the document object. const elem = document. activeElement; The activeElement returns the currently focused element in the document.
Supported elements HTML onblur attribute supports a, area, button, input, label, select, textarea elements.
Methods focus/blurblur() set/unset the focus on the element. It works in all browsers except Firefox (bug). If we enter something into the input and then try to use Tab or click away from the <input> , then onblur returns the focus back. Please note that we can't “prevent losing focus” by calling event.
The onblur event occurs when an object loses focus. The onblur event is most often used with form validation code (e.g. when the user leaves a form field). Tip: The onblur event is the opposite of the onfocus event.
Why not using focusout event? https://developer.mozilla.org/en-US/docs/Web/Events/focusout
relatedTarget property will give you the element that is receiving the focus.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With