Open In App

HTML DOM Input Search focus() Method

The Input Search focus() Method in HTML DOM is used to get focus to the Input search Field when an event occurs.  

Syntax:



searchObject.focus();

Parameter: This method does not contain any parameter value.

Return Value: It does not return any value.



Example: The below example illustrates the use of the Input search focus() method in HTML DOM.




<!DOCTYPE html>
<html>
 
<head>
    <title> Input Search Focus() Method </title>
    <style>
        h1 {
            color: green;
        }
         
        h2 {
            font-family: Impact;
        }
         
        body {
            text-align: center;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <h3>Input Search Focus() Method</h3>
    <form id="myGeeks">
        <input type="Search"
               id="test"
               name="myGeeks"
               placeholder="Type to search.."
               value="GeeksforGeeks" autofocus>
    </form>
    <br><br>
    <button onclick="Focus()"> Get Focus </button>
    <script>
        function Focus() {
            var s = document.getElementById("test").focus();
        }
    </script>
</body>
</html>

Output:

Input Search focus() Method

Supported Browser:


Article Tags :