The autofocus attribute is used to automatically focus on a <select> element when the webpage loads. It helps users interact with the dropdown menu quickly.
- Automatically places the cursor focus on the <select> element when the page opens.
- Improves user experience by reducing extra clicks.
- It is a boolean attribute, so its presence alone enables autofocus.
Syntax:
<select autofocus>Example: This example shows the autofocus attribute on a <select> element. The dropdown automatically receives focus when the page loads, allowing users to interact with it without clicking.
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
<title>
HTML select autofocus Attribute
</title>
</head>
<!--Driver Code Ends-->
<body style="text-align:center">
<h1 style="color: green;">
GeeksforGeeks
</h1>
<h2>
HTML <select> autofocus Attribute
</h2>
<select autofocus>
<option value="binary">
Binary Search
</option>
<option value="linear">
Linear Search
</option>
<option value="interpolation">
Interpolation Search
</option>
</select>
</body>
<!--Driver Code Starts-->
</html>
<!--Driver Code Ends-->