Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Focus fails on firefox

Tags:

jquery

firefox

I've been some testing reserching for this other question, when I noticed something very peculiar. FF4/5 fail to fire the focus jQuery event. The other question which might be considered a duplicate was closed and accepted without a real answer.

For the question itself, I tried the following simple bit of code:

$('#target').focusout(function() {
    $(this).focus();
});

It works well in Chrome and in IE, but it fails on FF. Here's the jsFiddle for the lazy ones among us.

Can anyone explain this behavior? Or is it a known bug?

like image 464
Madara's Ghost Avatar asked Aug 12 '11 22:08

Madara's Ghost


2 Answers

I think I ran into this before, and if I recall correctly it seemed to be some kind of reentrancy problem. My impression was that because FF is already in the process of transitioning focus, it won't let you initiate another focus transition. I believe my workaround was something like

$('#target').focusout(function() {
    setTimeout(function() {
        $(this).focus();
    }, 0);
});
like image 110
chaos Avatar answered Nov 16 '22 22:11

chaos


The manual says aboult .focus() call

This method is a shortcut for .trigger('focus')

and from the .trigger() topic

Although .trigger() simulates an event activation, complete with a synthesized event object, it does not perfectly replicate a naturally-occurring event.

So as I understand it, the call $(this).focus(); is supposed to trigger the OnFocus event (if there is one attached to the object) but is not quaranteed to actually set/change the focused object.

like image 39
ain Avatar answered Nov 17 '22 00:11

ain