Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't event.relatedTarget work with focusin/focusout event in Firefox?

I need to find the previously focused item in my focusin function. Here is an example code:

$('#id').on('focusin', function(event) {
  //console.log(event.relatedTarget.nodeName);    //doesn't work
}

I've done some research and while I've seen some people saying in posts that this only works with mouse events like mousedown etc., I've come across a few articles from reputable sources that have me believing this should work.

  1. https://developer.mozilla.org/en-US/docs/DOM/event.relatedTarget Here Firefox specifically mentions how event.relatedTarget returns "which EventTarget is losing focus" in the 'focusin' event. Firefox is the browser I am using for this.

  2. http://www.w3.org/TR/DOM-Level-3-Events/#events-FocusEvent at this bookmark you can see that every FocusEvent has a readonly attribute called related target.

  3. http://www.w3.org/TR/DOM-Level-3-Events/#event-type-focusIn Here they specifically state as well that the 'FocusEvent' has a property called 'relatedTarget' which is "event target losing focus (if any)."

So then what am I doing wrong here? It must be some kind of dumb syntax mistake or something. I cannot find the nodeName of event.relatedTarget.

Update: I can get it to work in IE using, but this won't work in Firefox???

 $("#id").on('focusin', function(event) {
   $('#textbox').text(event.relatedTarget.nodeName);
 }
like image 692
Klik Avatar asked Jan 31 '13 09:01

Klik


People also ask

Why is relatedTarget null?

relatedTarget can also be null when there was no previous/next target. This happens, for example, when a button had focus, and the user clicks something that is not focusable.

What is the difference between Blur and Focusout?

The main difference between this event and blur is that focusout bubbles while blur does not. The opposite of focusout is focusin .

How to use focusout in javascript?

The focusout event occurs when an element (or any elements inside it) loses focus. The focusout() method attaches a function to run when a focusout event occurs on the element, or any elements inside it. Unlike the blur() method, the focusout() method also triggers if any child elements lose focus.

What is Focusin event?

The focusin event fires when an element is about to receive focus. The main difference between this event and focus is that focusin bubbles while focus does not. The opposite of focusin is focusout . This event is not cancelable.


1 Answers

Although MDN mentions relatedTarget for the focusin /focusout events, unfortunately, no version of FireFox actually supports those two events. jQuery simply emulates them for you but due to lack of native support, you don't get relatedTarget on FF.

See compatibility info here or here.

like image 68
Dmitry Pashkevich Avatar answered Jan 29 '23 05:01

Dmitry Pashkevich