Klik
Klik

Reputation: 1776

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);
 }

Upvotes: 10

Views: 4580

Answers (1)

Dmitry Pashkevich
Dmitry Pashkevich

Reputation: 13536

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.

Upvotes: 9

Related Questions