onfocus event Event Object Example Execute a piece of JavaScript code when the input box gets focus: Try it » Definition and Usage The onfocus event occurs when an object gains focus. Onfocus is usually used for , , and. Tip: The opposite event of the onfocus event is the onblur event. Syntax in HTML: < element onfocus=" SomeJavaScriptCode "> In JavaScript: object .onfocus=function(){ SomeJavaScriptCode } In JavaScript, use the addEventListener() method: object .addEventListener("focus", myScript ); Try it Parameter description SomeJavaScriptCode Required. Specifies the JavaScript to be executed when this event occurs. Browser support All major browsers support the onfocus event HTML tags that support the onfocus event: The onfocus attribute is usually used on all HTML elements, except: , , , , , , , ,