Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML Tutorial
  • HTML Exercises
  • HTML Tags
  • HTML Attributes
  • Global Attributes
  • Event Attributes
  • HTML Interview Questions
  • HTML DOM
  • DOM Audio/Video
  • HTML 5
  • HTML Examples
  • Color Picker
  • A to Z Guide
  • HTML Formatter
Open In App

HTML DOM focus() Method

Last Updated : 13 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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: 

HTML
<!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: 

HTML
<!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 1.0 and above
  • Edge 12.0 and above
  • Internet Explorer 5.5 and above
  • Firefox 1.5 and above
  • Opera 8 and above
  • Safari 3 and above

B

bestharadhakrishna
Improve
Article Tags :
  • Technical Scripter
  • Web Technologies
  • HTML
  • Technical Scripter 2018
  • HTML-DOM

Similar Reads

    HTML DOM Style borderRadius Property
    The DOM Style borderRadius Property is used to set or return the four different borderRadius properties such as borderTopRightRadius, borderBottomRightRadius, and borderBottomLeftRadius of an element. It is used to add a rounded corner in an element. Syntax: It is used to get the border radius prope
    2 min read
    HTML DOM getAttribute() Method
    The HTML DOM getAttribute() method is used to retrieve the value of a specified attribute from an HTML element. It returns the attribute's value as a string or null if the attribute doesn't exist.Note: It will return a null or an empty string if the specified attribute doesn't exist.SyntaxObject.get
    2 min read
    HTML DOM parentElement Property
    The DOM parentElement property is used to return the parent element of a particular child element. It is a read-only property. The parentElement and parentNode properties are similar and the only difference is the parentElement property returns null if the parent node is not an element. Syntax: node
    2 min read
    HTML DOM previousSibling Property
    The previousSibling property is used to return the previous node of the specified node as Node object or null if the specified node is the first in the list. It is a read-only property. Syntax: node.previousSibling Return value: This property returns a previous sibling of the specified node or null
    1 min read
    HTML DOM innerText Property
    The DOM innerText Property is used to set or return the text content of a specified node and its descendants. This property is very similar to the text content property but returns the content of all elements, except for <script> and <style> elements. Syntax: It is used to set the innerT
    2 min read
    HTML DOM nextSibling Property
    The nextSibling property returns the next node at the same tree level, providing a node object. It's read-only and navigates through sibling nodes within the document structure. Syntax: node.nextSiblingReturn value: Name Description Node The nextSibling property returns the next sibling node or null
    2 min read
    HTML DOM isContentEditable Property
    The DOM isContentEditable property is used to return a boolean where true means the content of an element is editable and false represents content is not editable. This property is read-only. Syntax: Object.isContentEditable Return Value: This property returns a boolean value. true means that the co
    1 min read
    HTML DOM implementation Property
    The DOM implementation property in HTML is used to return the DOMImplementation object associated with the current document. The DOMImplementation is the interface that represents a method providing the object which is not dependent on any particular document. Syntax: document.implementation Return
    2 min read
    HTML DOM specified Property
    The DOM specified property is used to return the boolean value. It returns true if the element has a specified attribute, otherwise, it returns a false value if the element does not have a specific attribute. Syntax:attribute.specifiedReturn value: It returns the boolean value which represents wheth
    2 min read
    HTML DOM offsetLeft Property
    The DOM offsetLeft property is used to return the left position in pixels. This position is relative to the left side of the offsetParent element. Syntax: element.offsetLeft Return Value: It returns the number representing the left position of the element in pixels. Example: In this example, we will
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences