blur and focus

Test page.

Events overview page.

The focus and blur events keep track of the elements the user focuses on.

focus
Fires when an element gains the focus
blur
Fires when an element loses the focus

That sounds simple, but it isn’t.

Method or property IE 5.5 IE 6 IE 7 IE8b1 FF 2 FF 3 Saf 3.0 Win Saf 3.1 Win iPhone 3G Opera 9.26 Opera 9.51 Konqueror 3.5.7
On the window
Yes Yes Yes No Minimal Yes No

Are the focus and blur events available on the window?

  • Opera allows registration on the window only when you use attachEvent.
On the document
No Too many No Yes Minimal

Are the focus and blur events available on the document?

  • Firefox fires a focus event whenever you click on the document.
  • Konqueror only fires these events on the document when you use addEventListener in the capturing phase.
On links

There should be focus and blur events on the <a> only.

Yes Too many Yes No Incomplete No No Too many Yes

Focus and blur events on links should always fire, regardless of whether the user uses the mouse or the keyboard.

  • Firefox 2 also fires events on the document and window.
  • Safari only fires the events when the user uses the keyboard.
  • Opera always fires a focus and a blur event immediately. When the user uses the keyboard a second focus event is fired.
Method or property IE 5.5 IE 6 IE 7 IE8b1 FF 2 FF 3 Saf 3.0 Win Saf 3.1 Win iPhone 3G Opera 9.26 Opera 9.51 Konqueror 3.5.7
On form fields

There should be focus and blur events on the form field only.

Yes Too many Yes Incomplete Yes Too many Yes

Focus and blur events on form fields should always fire, regardless of whether the user uses the mouse or the keyboard.

  • Firefox 2 also fires events on the form, document and window when the event target is a radio, checkbox, select or button. Text fields and textareas work correctly.
  • Safari does not fire focus/blur events when the user uses the mouse to access checkboxes, radios or buttons. Text fields, textareas and select boxes work correctly.
  • When using the keyboard, Opera 9.5b fires blur (but not focus) events on the document and window, too.
  • iPhone note: When you remove the keyboard after filling in a form field, the blur event fires. This is consistent with the interface.
On focusable elements

By giving it a tabindex you can make any element you like focusable. The focus and blur events should work on such elements.

Yes Yes Untestable Untestable Yes Untestable

tabindex does not work in Safari, Opera 9.26 and Konqueror.

Focusing and blurring the entire window

This action should fire a focus and blur event on the window only.

Yes Almost Too many Too many Yes No Incorrect Too many No

If the user brings a browser window forward (by clicking on it, using Alt+Tab or manipulating the taskbar), a focus event on the window fires. Likewise, if the browser window loses focus, a blur event fires.

  • IE8b1 misses the first blur event on the window.
  • Firefox 2 fires too many focus and blur events.
  • Safari 3.0 fires too many focus events.
  • Firefox and Opera 9.5b fire the events on the document and the window.
  • Opera 9.26 fires the event on the document only.
Method or property IE 5.5 IE 6 IE 7 IE8b1 FF 2 FF 3 Saf 3.0 Win Saf 3.1 Win iPhone 3G Opera 9.26 Opera 9.51 Konqueror 3.5.7
Focusing and blurring the “default space”

No event should fire.

Personally I feel that, if the “default space” should be represented at all, it should be represented by just the document object.

Alternative Yes Probably Yes Untestable Yes Largely untestable

What happens when the focus passes from a link or form field to the document in general, or vice versa?

Suppose the focus is on a form field, and the user clicks nowhere in particular? The form field is blurred, but is anything focused?

Or suppose the focus is on the document in general, and the user focuses on a link. The link is focused, but is anything blurred?

I call the document area that is not a link or a form field the “default space”. There are two theories that explain the “default space’s” connection with the links and form fields it contains.

  1. The first theory pictures the “default space” as the “parent” of all links and form fields. Therefore, if the focus is on the “default space” and is moved to a link or form field, the “default space” does not lose the focus; the focus is just more narrowly defined.
    Therefore the “default space” should not fire blur and focus events if the user passes the focus from it to a link or form field or vice versa.
  2. The second theory pictures the “default space” as a cover of the entire page with “holes” cut for every link and form field element. The “default space” and the various “holes” are equal partners; and only one of them can hold the focus.
    Therefore the “default space” should fire blur and focus event as if it were just another link or form field.
    Then there's the additional question which JavaScript object represents the “default space”.

As you might have guessed, browsers disagree on which theory to follow:

  • IE8, Safari, and Opera follow the first theory.
  • Firefox probably follows the first theory, but the bug for these events on the document makes judging its compatibility difficult.
  • IE5-7 follows the second theory and uses window as representation of the “default space”.
  • Konqueror follows the second theory when you use addEventListener in the capturing phase. In all other registration models the events are not available on the document and Konqueror becomes untestable.
Event bubbling
“No” is the correct answer

The focus and blur events do not bubble up.

No Incorrect No No No No

Note that focus and blur events can be captured.

  • In Firefox 2 the events seem to bubble up from links, radios, checkboxes, selects and buttons. This cannot be prevented.
Prevent default
“No” is the correct answer
No No No No No

There is no default to prevent: the events are fired once the user has succesfully put the focus on an element.

Related events

Method or property IE 5.5 IE 6 IE 7 IE8b1 FF 2 FF 3 Saf 3.0 Win Saf 3.1 Win iPhone 3G Opera 9.26 Opera 9.51 Konqueror 3.5.7
activate

Despite the name this event is an equivalent of focusin.

Test page.

Yes No No No No
deactivate

Despite the name this event is an equivalent of focusout.

Test page.

Yes No No No No
DOMFocusIn

As focus, but bubbles.

Test page.

No No No Incomplete No Yes Yes
  • Safari doesn’t fire the event when you focus on links or elements with tabindex.
DOMFocusOut

As blur, but bubbles.

Test page.

No No No Incomplete No Yes Yes
  • Safari doesn’t fire the event when you focus on links or elements with tabindex.
focusin

As focus, but bubbles.

Test page.

No Yes No No No No

Not available on the window.

focusout

As blur, but bubbles.

Test page.

No Yes No No No No

Not available on the window.

Method or property IE 5.5 IE 6 IE 7 IE8b1 FF 2 FF 3 Saf 3.0 Win Saf 3.1 Win iPhone 3G Opera 9.26 Opera 9.51 Konqueror 3.5.7