🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Create JQuery Autocomplete Text Box With Example

By Siddharth NandavaUpdated on Mar 17, 2025 4 min read

JavaScript adds life to the UI of online applications and web forms. 💫

You can use JS to create highly interactive online experiences, which would have been challenging using HTML & CSS solely. The jQuery UI library boasts plenty of interactions, widgets, themes, effects, and utilities that you can readily use. One such useful widget is jQuery Autocomplete.

Think about how Google saves your time with autocomplete suggestions while searching online.

jQuery Autocomplete can help you implement similar functionality in your web app or form. Offering such functionality to the users can help you:

  • Smoothen User Experience
  • Reduce Cognitive Demand
  • Avoid Typing Mistakes

You can use the jQuery Autocomplete widget with the HTML elements: textbox, textarea, and input. It is triggered on a key-up event and shows the autocomplete suggestions to the users whenever they type something in the field.

In this blog post, I am going to show you jQuery Autocomplete textbox select example through array and database. 😃

jQuery Autcomplete Example + Code

In this example, I’ve used the jQuery Autocomplete widget to fetch suggestions for the states and territories of the United States. The function will be triggered when you start typing in the text box. For example, on typing the letters ‘al’ in the textbox, the jQuery Autocomplete widget searches the entire array or database (if connected) and fetches all the matching results that contain the entered text, which are ‘Alabama’ and ‘Alaska’ in this case.

Textbox showing autocomplete suggestions using jQuery autocomplete

In the above example, I used an array to declare the list of all the states & territories of the United States. You can take the following code for reference:

<!doctype html>
  
  
  
  
  <script src="https://code.jquery.com/jquery-3.6.0.js"></script><script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script><script>
  $( function() {
    var availableStates = ["Alabama",
"Alaska",
"Arizona",
"Arkansas",
"California",
"Colorado",
"Delaware",
"Florida",
"Georgia",
"Hawaii",
"Idaho",
"Illinois",
"Indiana",
"Kansas",
"Kentucky",
"Louisiana",
"Maine",
"Maryland",
"Massachusetts",
"Michigan",
"Minnesota",
"Mississippi",
"Missouri",
"Montana",
"Nebraska",
"Nevada",
"New Hampshire",
"New Jersey",
"New Mexico",
"New York",
"North Carolina",
"North Dakota",
"Washington",
"West Virginia",
"Wisconsin",
"Wyoming"
    ];
    $( "#states" ).autocomplete({
      source: availableStates
    });
  } );
  </script>

jQuery Autocomplete From Database

Instead of using an array, you can also set database as a source for showing autocomplete suggestions. For example, I used the below code to fetch the jQuery autocomplete from the database using JSON encoding:

var availableProducts = <!--?php echo json_encode($products);?-->;

$(document).on('focusout keyup', '.invoice_item', function (index, value) {
    boxId = jQuery(this).attr('id');
    $("#"+boxId).autocomplete({
        source: availableProducts,
    });
});

That’s it! This is how you can use the jQuery Autocomplete widget to show input suggestions in textfield.

If you still have doubts regarding this solution, you can mention them in the comments; I’d be glad to help you! 😇

Earlier, my colleague showed you a method to reload current page without losing any form data through JavaScript, which you’d like to implement in your web app or form.

Loved this post? Rate it with 5 stars and share it with your techie friends! 😃

Thanks for reading! 🍀

Siddharth Nandava Full Image
Article bySiddharth Nandava

Siddharth Nandava is an enthusiastic Jr Magento developer at Meetanshi. Apart from the work, you can find him learning new things and spending quality time with his family.