, , , , , , , , and
Event Onfocusin
[ Event Object](#)
## Example
Execute JavaScript when an input field is about to receive focus:
[Try it Β»](#)
More examples are available at the bottom of this chapter.
* * *
## Definition and Usage
The onfocusin event occurs when an element is about to receive focus.
**Note:** The onfocusin event is similar to the (#) event. The main difference is that the onfocus event does not bubble. Therefore, if you want to know if an element or its child elements receive focus, you need to use the onfocusin event.
**Note:** Although the Firefox browser does not support the onfocusin event, you can use the (#) event with the optional _useCapture_ parameter of the addEventListener() method to detect if an element or its child elements receive focus.
**Note:** The opposite event of onfocusin is the (#) event.
* * *
## Browser Support
| Event | | | | | |
| --- | --- | --- | --- | --- | --- |
| onfocusin | Yes | Yes | Not Supported | Yes | Yes |
**Note:** The onfocusin event using HTML DOM syntax may not work properly in Chrome, Safari, and Opera 15+ browsers. However, it works as an HTML element by using the addEventListener() method.
* * *
## Syntax
In HTML:
(#)
In JavaScript (may not work in Chrome, Safari, and Opera 15+):
_object_.onfocusin=function(){_myScript_};(#)
In JavaScript, using the addEventListener() method:
_object_.addEventListener("focusin", _myScript_);(#)
**Note:** Internet Explorer 8 and earlier versions of IE do not support the [addEventListener()](#) method.
* * *
## Technical Details
| Bubbles: | Yes |
| --- |
| Cancelable: | No |
| Event type: | FocusEvent |
| Supported HTML tags: | All HTML elements, except: , ,
, , , , , , , , and |
* * *

## More Examples
## Example
Using the "onfocusin" and "onfocusout" events together:
[Try it Β»](#)
## Example
Event delegation: Setting the _useCapture_ parameter of addEventListener() to true (for focus and blur):
var x = document.getElementById("myForm");
x.addEventListener("focus", myFocusFunction, **true**);
x.addEventListener("blur", myBlurFunction, **true**);
function myFocusFunction() {
document.getElementById("myInput").style.backgroundColor = "yellow";
}
function myBlurFunction() {
document.getElementById("myInput").style.backgroundColor = "";
}
[Try it Β»](#)
## Example
Event delegation: Using the focusin event (not supported in Firefox):
var x = document.getElementById("myForm");
x.addEventListener("focusin", myFocusFunction);
x.addEventListener("focusout", myBlurFunction);
function myFocusFunction() {
document.getElementById("myInput").style.backgroundColor = "yellow";
}
function myBlurFunction() {
document.getElementById("myInput").style.backgroundColor = "";
}
[Try it Β»](#)
* * Event Object](#)
, , , , , , , , and
YouTip