Open In App

HTML DOM Input Text focus() Method

Last Updated : 31 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The Input Text focus() method in HTML DOM is used to get focus to the text field when the event occurs. 

Syntax:

textObject.focus()

Parameters: This method does not contain any parameter values. 

Return Value: It does not return any value.

Example: Below program illustrates the use of the input text focus()  method in HTML DOM. 

HTML
<!DOCTYPE html>
<html>

<head>
    <title>
        HTML DOM Input Text focus() Method
    </title>
</head>

<body style="text-align:center;">
    <h1 style="color:green">
        GeeksforGeeks
    </h1>

    <strong>DOM Input Text focus() method</strong>
    
    <form id="myForm">
        <input type="text" id="text_id" name="userinput">
    </form>
    <br>
    <button onclick="myfocus()">Get Focus!</button>

    <!-- script to get focus to the text field-->
    <script>
        function myfocus() {
            var txt = document.getElementById("text_id").focus();
        }
    </script>
</body>

</html>                  

Output:

HTML DOM Input Text focus() Method
HTML DOM Input Text focus() Method

Supported Browsers:

  • Google Chrome 1
  • Mozilla Firefox 1
  • Edge 12
  • Opera
  • Safari 1

Next Article

Similar Reads