Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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
Next Article:
Set the focus to HTML form element using JavaScript
Next article icon

How to focus element while loading the page in HTML ?

Last Updated : 29 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

HyperText Markup Language(HTML) is the basic building block of web pages that defines a structure. This markup language is used by browsers to manipulate data like text, images, and other content so that they can be displayed in the required format. HyperText refers to the links that connect web pages and markup defines the text document within the tags.

How to focus the element while loading the page in HTML?

We want that an element should be in focus whenever we load the page. We can do this in 2 ways:

  • Using autofocus attribute of the element.
  • Using Window: load event.

Let's explore both ways with examples.

1. Using autofocus attribute The autofocus  attribute is a boolean attribute i.e it can only be true or false. When this attribute is present inside an element, it specifies that the element inside which it is present should automatically get focus whenever the page loads.

The autofocus attribute can be only used within the below mentioned elements -

  • <input>
  • <button>
  • <textarea>
  • <select>

Syntax: 

<elementName autofocus>

Example: In this example, whenever the page loads the input element gets focus and we can see it as the background color of input changes to #8ecae6.

HTML
<!DOCTYPE html> <html>  <head>     <title>Using autofocus attribute</title>     <link rel="stylesheet" href="style.css"> </head>  <body>     <div>         <p>GeeksforGeeks</p>          <input type="text" placeholder=             "Hey Geeks, Type something here"              autofocus>     </div> </body>  </html> 
CSS
* {     margin: 0;     padding: 0;     box-sizing: border-box; }  div {     margin: auto;     margin-top: 10px;     height: 100px;     width: 300px;     background-color: green;     text-align: center;     font-size: 24px;     font-family: Arial, Helvetica, sans-serif; }  p {     padding-top: 15px;     color: whitesmoke; }  input {     margin-top: 15px;     width: 75%;     color: black;     font-weight: bolder; }  input:focus {     background-color: #8ecae6; } 

Output: 

1

2. Using Window: load Event: The load event of window object is fired when the complete page has loaded including stylesheets and images. Now when the page has been loaded , we can use HTMLElement.focus() method to provide focus to our element(if can be focused). The focused element is the one that receive keyboard and similar events by default.

Syntax:

HTMLElement.focus();

The focus() method can have an optional parameter that is an object providing options to control aspects of the focusing process.

Example: 

HTML
<!DOCTYPE html> <html>  <head>     <title>Using load event</title>     <link rel="stylesheet" href="style.css"> </head>  <body>     <div>         <p>GeeksforGeeks</p>         <input type="text" placeholder=             "Hey Geeks, Type something here">     </div>     <script src="script.js"></script> </body>  </html> 
CSS
* {     margin: 0;     padding: 0;     box-sizing: border-box; }  div {     margin: auto;     margin-top: 10px;     height: 100px;     width: 300px;     background-color: green;     text-align: center;     font-size: 24px;     font-family: Arial, Helvetica, sans-serif; }  p {     padding-top: 15px;     color: whitesmoke; }  input {     margin-top: 15px;     width: 75%;     color: black;     font-weight: bolder; }  input:focus {     background-color: #8ecae6; } 
JavaScript
let inputElem = document.querySelector("input"); window.addEventListener('load', function(e) {     inputElem.focus(); }) 

Output: 

1

Supported Browsers:


Next Article
Set the focus to HTML form element using JavaScript

S

siddharth01
Improve
Article Tags :
  • Web Technologies
  • HTML
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • HTML-Questions

Similar Reads

  • How to pre-select an input element when the page loads in HTML5?
    In this article, we are going to pre-select an input element when the page loads i.e. the user doesn't have to click the first text box to start writing on it. When the page is loaded the input is to be pre-selected and the user can start typing directly. This can be done by using the autofocus attr
    1 min read
  • Set the focus to HTML form element using JavaScript
    To set focus to an HTML form element, there can be many methods that can be used to focus. In this article, we will discuss them all. These are the following approaches: Table of Content Using focus() methodUsing window.onload methodApproach 1: Using focus() methodIn this approach, we are going to u
    2 min read
  • How to set a keygen element that automatically get focused when page loads in HTML5 ?
    The <keygen> tag in HTML is used to specify a key-pair generator field in a form. The purpose of this element is to provide a secure way to authenticate users. When a form is submitted then two keys are generated, private key and public key. The private key is stored locally, and the public ke
    1 min read
  • How to set the focus on drop-down list automatically when the page loads in HTML5?
    The purpose of this article is to focus the drop-down list automatically when the page loads in HTML5. The <select> autofocus attribute is a boolean attribute that specifies that the drop-down list should automatically get focus when the page loads. Syntax: <select autofocus> Example: Th
    1 min read
  • How to Link a Button to Another Page in HTML?
    Linking a button to another page is a common requirement in web development. It can allow the users to navigate between a website's different sections or pages. This can be done using the various HTML elements. PrerequisitesHTMLCSS1. Using <a> Tag Styled as ButtonThe <a> tag is tradition
    3 min read
  • How to get focused a button automatically when the page load using HTML ?
    The <button> tag in HTML is used to define the clickable button. The <button> tag is used to submit the content. The images and text content can use inside <button> tag. The <button> autofocus attribute is used to get focused button automatically after loading the web pages.
    1 min read
  • Which event will be triggered when element get focused in HTML ?
    When the user interacts with the HTML elements, HTML has the ability to let events trigger actions in a browser, such as a user clicks on an element, focus in or focus out of an element etc. The events that are related to focus are contained within the DOM FocusEvent Object. The onfocus will be trig
    2 min read
  • How to use anchor tag to open links in HTML ?
    In this article, we will learn how to use anchor tags to open links in HTML and we will implement different operations using anchor tags in HTML. We will see in this article, how to redirect another website through an anchor tag and how to open a blank new tag for any new link to open an image in a
    3 min read
  • How to Change the Target of a Link in HTML ?
    In HTML, for changing the target of a link we can use the target attribute in the <a> anchor tag. This attribute mainly specifies where the linked content should be displayed. Below are the approaches that can be used to change the target link in HTML: Table of Content Using target Attribute O
    3 min read
  • How to preload the video when the page loads in HTML5?
    The HTML <preload> attribute is used to specify the way the developer thinks the video should be loaded when the page loads. Syntax<video preload=""> </video>There are 3 values for preload attribute auto, metadata and none. Note: An empty string leads to the default auto attribute
    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