The onfocus property of the GlobalEventHandlers mixin is an EventHandler that processes focus events on the given element.

The focus event is raised when the user sets focus on an element.

For onfocus to fire on non-input elements, they must be given the tabindex attribute (see Building keyboard accessibility back in for more details).

Note: The opposite of onfocus is onblur.


target.onfocus = functionRef;


functionRef is a function name or a function expression. The function receives a FocusEvent object as its sole argument.


This example uses onblur and onfocus to change the text within an <input> element.


<input type="text" value="CLICK HERE">


let input = document.querySelector('input');

input.onblur = inputBlur;
input.onfocus = inputFocus;

function inputBlur() {
  input.value = 'Focus has been lost';

function inputFocus() {
  input.value = 'Focus is here';


Try clicking in and out of the form field, and watch its contents change accordingly.


Specification Status Comment
HTML Living StandardThe definition of 'onfocus' in that specification. Living Standard  

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
onfocus Chrome

Full support Yes


Full support 12


Full support Yes


Full support Yes


Full support Yes


Full support Yes

WebView Android

Full support Yes

Chrome Android

Full support Yes

Firefox Android

Full support Yes

Opera Android

Full support Yes

Safari iOS

Full support Yes

Samsung Internet Android

Full support Yes


Full support  
Full support

In contrast to IE, in which almost all kinds of elements receive the focus event, almost all kinds of elements on Gecko browsers do NOT work with this event.

See also