
- HTML - Home
- HTML - Roadmap
- HTML - Introduction
- HTML - History & Evolution
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Headings
- HTML - Paragraphs
- HTML - Fonts
- HTML - Blocks
- HTML - Style Sheet
- HTML - Formatting
- HTML - Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM Element focus() Method
The HTML DOM Element focus() method is used to set focus on a specific element in the web page, such as input fields or button elements.
The term focus refers to the active element that receives input from the keyboard or other input devices. This method allows you to set any element as active or focused based on certain criteria.
Syntax
Following is the syntax of the HTML DOM Element focus() method −
element.focus();
Parameters
This method does not accept any parameter.
Return Value
This method does not return any value.
Example 1: Setting Auto Focus on an Input Field
The following is the basic example of the HTML DOM Element focus() method. It sets auto focus on an input field −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element focus()</title> </head> <body> <h3>HTML DOM Element focus() Method</h3> <p>Setting auto focus to an Input Field</p> <form onsubmit = "return false"> <label for="username">Username:</label> <input type="text" id="username" name="username"> </form> <script> function focusUsername() { var usernameInput = document.getElementById('username'); usernameInput.focus(); } focusUsername(); </script> </body> </html>
The above program sets the auto focus on an input field after loading the page.
Example 2: Highlighting Second Input Field on Focus
Here is another example of the HTML DOM Element focus() method. We use this method to highlight the second input field when it gets focused −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element focus()</title> <style> .highlight{ background-color: yellow; } </style> </head> <body> <h3>HTML DOM Element focus() Method</h3> <p>Focus on Second Input Field Example</p> <form> <input type="text" id="firstName" placeholder="First name"> <br><br> <input type="text" id="lastName" placeholder="Last name"> <br><br> <input type="email" id="email" placeholder="Email"> <br><br> <button type="button" onclick="focusOnFirstInput()">Focus on second Field </button> </form> <script> function focusOnFirstInput() { // Get all input elements within the form const formInputs = document.querySelectorAll('form input'); // Focus on the second input element formInputs[1].focus(); formInputs[1].classList.add("highlight"); } </script> </body> </html>
When the button is clicked, the second input field will be focused and highlighted with a yellow background.
Example 3: Setting Focus on Button Element
In the example below, we use the focus() method to set focus on a specific button (i.e., the second button) on the web page −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element focus()</title> <style> .highlight { background-color: green; color: white; padding: 10px; } </style> </head> <body> <h3>HTML DOM Element focus() Method</h3> <p>The first button will be auto focused after 2 seconds.</p> <form> <input type="text" id="firstName" placeholder="First name"> <br><br> <input type="text" id="lastName" placeholder="Last name"> <br><br> <input type="email" id="email" placeholder="Email"> <br><br> <button type="button" onclick="focusOnFirstInput()">Submit</button> <button type="button">Reset</button> <button type="button">Refresh</button> <span id="res"></span> </form> <script> setInterval(setFocus, 2000); function setFocus() { let btns = document.querySelectorAll('button'); btns[0].focus(); btns[0].classList.add('highlight'); document.getElementById('res').innerHTML = "Focused..!"; } </script> </body> </html>
Once the above program is executed, the first button will be auto-focused after 2 seconds of the page being loaded.
Supported Browsers
Method | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
focus() | Yes | Yes | Yes | Yes | Yes |