Open In App
Related Articles

jQuery UI Autocomplete autoFocus Option

Improve Article
Improve
Save Article
Save
Like Article
Like

In this article, we will go through about Autocomplete autoFocus option in JqueryUI. jQuery UI Autocomplete autoFocus option when set to true, the first item will automatically be focused when the menu is shown. The default value is False

Syntax:

$( ".selector" ).autocomplete({autoFocus: true }),

Approach: First, add jQuery Mobile scripts needed for your project.

<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js”></script> 
<script src=”https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js”></script> 
<link href=”http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/ui-lightness/jquery-ui.css” rel=”stylesheet” type=”text/css” />

Example 1:

HTML




<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1">
    <script src=
    </script>
    <script src=
    </script>
    <link href=
          rel="stylesheet"
          type="text/css" />
 
    <script>
        $(function() {
           var list  =  [
              "One",
              "two",
              "Three",
              "Four",
           ];
           $( "#gfg" ).autocomplete({
              source: list,
              autoFocus:false
           });
        });
    </script>
</head>
 
<body>
    <div class="ui-widget">
        <p>jQuery UI| autoFocus Element</p>
        <p>GeeksforGeeks</p>
        <label for="gfg">Tags: </label>
        <input id="gfg">
    </div>
</body>
</html>


Output:

Example 2:

HTML




<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1">
 
    <script src=
    </script>
    <script src=
    </script>
 
    <link href=
          rel="stylesheet"
          type="text/css" />
    <script>
        $(function() {
           var list  =  [
              "One",
              "two",
              "Three",
              "Four",
           ];
           $( "#gfg" ).autocomplete({
              source: list,
              autoFocus:true
           });
        });
    </script>
</head>
 
<body>
    <div class="ui-widget">
        <p>jQuery UI| autoFocus Element</p>
        <p>GeeksforGeeks</p>
        <label for="gfg">Tags: </label>
        <input id="gfg">
    </div>
</body>
</html>


Output:


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 04 Apr, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials