Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the newly focussed element (if any) from the onBlur event.

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.

like image 815
fadedbee Avatar asked Jul 21 '12 14:07

fadedbee


People also ask

How do you find the focus element?

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.

What element is Onblur?

Supported elements HTML onblur attribute supports a, area, button, input, label, select, textarea elements.

How do you handle focus and blur events?

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.

What is the Onblur 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.


1 Answers

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.

like image 136
Diego Hueltes Avatar answered Nov 02 '22 23:11

Diego Hueltes