HTML | DOM onfocusin Event
The HTML DOM onfocusin event occurs when an element is getting focused. The onfocusin is same as onfocus, the only difference is that onfocus event does not bubble.
If you want to find out whether an element or its child gets the focus, you should use the onfocusin event.
The onfocusin event is the opposite of the onfocusout event.
Note: Firefox does not support the onfocusin event but with the help of capturing listener you can find out whether a child of an element gets the focus or not.
Syntax:
In HTML:
<element onfocusin="myScript">
In JavaScript (may not work as expected in Chrome, Safari and Opera 15+):
object.onfocusin = function(){myScript};
In JavaScript, using the addEventListener() method:
object.addEventListener("focusin", myScript);
Example: Using HTML
<!DOCTYPE html> <html> <body> <center> <h1 style="color:green">GeeksforGeeks</h1> <h2>HTML DOM onfocusin Event</h2> Focus: <input type="text" onfocusin="GFGfun(this)"> <script> function GFGfun(foc) { foc.style.background = "yellow"; } </script> </center> </body> </html> |
Output:

Example: Using JavaScript
<!DOCTYPE html> <html> <body> <center> <h1 style="color:green">GeeksforGeeks</h1> <h2>HTML DOM onfocusin Event</h2> Focus: <input type="text" id="fname"> <script> document.getElementById( "fname").onfocusin = function() { myFunction() }; function myFunction() { document.getElementById( "fname").style.backgroundColor = "yellow"; } </script> </center> </body> </html> |
Output:

Example: Using the addEventListener() method:
<!DOCTYPE html> <html> <body> <center> <h1 style="color:green">GeeksforGeeks</h1> <h2>HTML DOM onfocusin Event</h2> Focus: <input type="text" id="fname"> <script> document.getElementById( "fname").addEventListener("focusin", gfgFun); function gfgFun() { document.getElementById( "fname").style.backgroundColor = "yellow"; } </script> </center> </body> </html> |
Output:

Supported Browsers: The browsers supported by HTML DOM onfocusin Event are listed below:
- Google Chrome
- Internet Explorer
- Firefox 52
- Apple Safari
- Opera
Recommended Posts:
- HTML | DOM onreset Event
- HTML | DOM fullscreenchange Event
- HTML | DOM onscroll Event
- HTML | DOM onunload Event
- HTML | DOM onerror Event
- HTML | DOM onafterprint Event
- HTML | DOM onblur Event
- HTML | DOM onresize Event
- HTML | DOM animationend Event
- HTML | DOM onmouseleave Event
- HTML | DOM oncut Event
- HTML | DOM onloadstart Event
- HTML | DOM onbeforeunload Event
- HTML | DOM oncontextmenu Event
- HTML | DOM onmouseup Event
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



