jQuery Autocomplete enables users to quickly find and select from a pre-populated list of values as they type, leverage, search and filter. Autocomplete can be done with both client side data and server side. Client side means we fetch data only once then the filter will be applied on that data. Server side means as and when user types it goes to server and fetches relevant data.
Client Side Autocomplete:
Javascript:
- $(document).ready(function()
- {
- //simple javascript string array
- var availableKeywords = [
- "ActionScript",
- "BASIC",
- "C",
- "C++",
- "Erlang",
- "Fortran",
- "Groovy",
- "Haskell",
- "Java",
- "JavaScript",
- "Python",
- "Ruby",
- "Scala",
- "Scheme"];
- //Initialize autocomplete with the above array
- $("#keywords").autocomplete({
- source: availableKeywords
- });
- });
- <div class="ui-widget">
- <label for="keywords">Keywords: </label>
- <input id="keywords">
- </div>
Autocomplete with Remote Data:
Javascript:
- $("#cities").autocomplete({
- source: "search.php", //Url which serves the request
- minLength: 2, //minimum characters needed to request data from server
- select: function(event, ui)
- {
- //do something on selection
- }
- });
- <div class="ui-widget">
- <label for="cities">Cities: </label>
- <input id="cities">
- </div>
Join the conversation! Your thoughts help the community grow.