HTML | DOM focus() Method
DOM focus() method is used to give focus to an element and also to remove the focus with the help of blur() method. We can apply focus to any element and enable it by doing some operation. For example, we can give focus to some text by clicking a button.
Syntax:
Object.focus()
Example-1:
<!DOCTYPE html> <html> <head> <title>DOM focus() Method</title> <style> input[type=text]:focus { background-color: red; } </style> </head> <body> <center> <input type="text" id="gfg" value="GeeksForGeeks"> <h2>DOM focus() Method </h2> <button type = "button" onclick = "geek()"> Submit </button> <script> function geek() { document.getElementById("gfg").focus(); } </script> </center> </body> </html> |
Output:
Before applying focus:

After applying focus:

Example-2:
<!DOCTYPE html> <html> <head> <title>DOM focus() Method</title> <style> a:focus { background-color: magenta; } </style> </head> <body> <center> <h1 style="color:green;"> GeeksForGeeks </h1> <h2>DOM focus() Method</h2> <a href="www.geeksforgeeks.com" id = "geek">geeksforgeeks</a> <br> <button type = "button" onclick = "geeks()"> Submit </button> <script> function geeks() { document.getElementById("geek").focus(); } </script> </center> </body> |
Output:
Before applying focus:

After applying focus:

Supported Browsers: The browser supported by DOM focus() method are listed below:
- Google Chrome 15.0
- Internet Explorer 11.0
- Firefox 15.0
- Opera 15.0
- Safari 6.0
Recommended Posts:
- Set the focus to HTML form element using JavaScript
- Javascript | Window Blur() and Window Focus() Method
- CSS | :focus Selector
- jQuery | :focus Selector
- AngularJS | ng-focus Directive
- How to remove focus around buttons on click?
- Difference between :focus and :active selector
- Implement a JavaScript when an element loses focus
- JQuery | Set focus on a form input text field on page load
- HTML | DOM contains() Method
- HTML | DOM hasChildNodes() Method
- HTML | DOM getBoundingClientRect() Method
- HTML | DOM removeAttributeNode() Method
- HTML | DOM normalizeDocument() Method
- HTML | DOM getNamedItem() Method
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.
Improved By : shubham_singh



