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.

Syntax:

<button type="" autofocus> Submit <button>

Example 1:

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
  
    <h3>
        How to specify that a button 
        should automatically get 
        focus when the page load?
    </h3>
  
    <!-- button tag starts from here -->
    <button type="button" autofocus 
        onclick="alert('Welcome to GeeksforGeeks')">
        Click Here
    </button>
    <!-- button tag ends here -->
  
</body>
</html>

chevron_right


Output:



  • On click of the submit button, the following alert message box is shown to the user.

Example 2:

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
    <h3>
        How to specify that a button 
        should automatically get 
        focus when the page load?
    </h3>
  
    <!-- button tag starts from here -->
    <button type="button" autofocus onclick="myFun()">
        Click Here
    </button>
    <!-- button tag ends here -->
  
    <p id=result></p>
  
    <script>
        function myFun()
        {
            document.getElementById("result")
               .innerHTML = "Welcome to GeeksforGeeks";
        }
    </script>
</body>
</html>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.