api.jqueryui.com
Open in
urlscan Pro
2606:4700::6811:3b5e
Public Scan
Submitted URL: http://docs.jquery.com/UI/Autocomplete#theming
Effective URL: https://api.jqueryui.com/autocomplete/
Submission: On January 08 via api from US — Scanned from DE
Effective URL: https://api.jqueryui.com/autocomplete/
Submission: On January 08 via api from US — Scanned from DE
Form analysis
1 forms found in the DOMGET https://api.jqueryui.com/
<form role="search" class="searchform tsmb-form tsmb-form--slash" action="https://api.jqueryui.com/" method="get" data-origin="https://typesense.jquery.com" data-collection="jqueryui_com" data-key="Zh8mMgohXECel9wjPwqT7lekLSG3OCgz" data-foot="true"
data-group="true">
<input type="search" name="s" aria-label="Search jQuery UI API Documentation" value="" placeholder="Search" autocomplete="off">
<div role="listbox" hidden=""></div>
<button type="submit" class="visuallyhidden"></button>
<svg viewBox="0 0 12 12" width="20" height="20" aria-hidden="true" class="tsmb-icon-close" style="display: none;">
<path d="M9 3L3 9M3 3L9 9"></path>
</svg>
</form>
Text Content
* jQuery * jQuery UI * jQuery Mobile * Sizzle * QUnit * Plugins * Contribute * CLA * Style Guides * Bug Triage * Code * Documentation * Web Sites * Events * Support * Learning Center * IRC/Chat * Forums * Stack Overflow * Commercial Support * OpenJS Foundation * Join * Members * Governance * Conduct * Donate JQUERY UI API DOCUMENTATION * Demos * Download * API Documentation * Themes * Development * Support * Blog * About Navigate...DemosDownloadAPI DocumentationThemesDevelopmentSupportBlogAbout AUTOCOMPLETE WIDGET -------------------------------------------------------------------------------- Categories: Widgets AUTOCOMPLETE WIDGETVERSION ADDED: 1.8 Description: Autocomplete enables users to quickly find and select from a pre-populated list of values as they type, leveraging searching and filtering. QUICKNAVEXAMPLES OPTIONS appendTo autoFocus classes delay disabled minLength position source METHODS close destroy disable enable instance option search widget EXTENSION POINTS _renderItem _renderMenu _resizeMenu EVENTS change close create focus open response search select Any field that can receive input can be converted into an Autocomplete, namely, <input> elements, <textarea> elements, and elements with the contenteditable attribute. When typing in the autocomplete field, the plugin starts searching for entries that match and displays a list of values to choose from. By entering more characters, the user can filter down the list to better matches. This can be used to choose previously selected values, such as entering tags for articles or entering email addresses from an address book. Autocomplete can also be used to populate associated information, such as entering a city name and getting the zip code. You can pull data in from a local or remote source: Local is good for small data sets, e.g., an address book with 50 entries; remote is necessary for big data sets, such as a database with hundreds or millions of entries to select from. To find out more about customizing the data source, see the documentation for the source option. KEYBOARD INTERACTION When the menu is open, the following key commands are available: * UP: Move focus to the previous item. If on first item, move focus to the input. If on the input, move focus to last item. * DOWN: Move focus to the next item. If on last item, move focus to the input. If on the input, move focus to the first item. * ESCAPE: Close the menu. * ENTER: Select the currently focused item and close the menu. * TAB: Select the currently focused item, close the menu, and move focus to the next focusable element. * PAGE UP/PAGE DOWN: Scroll through a page of items (based on height of menu). It's generally a bad idea to display so many items that users need to page. When the menu is closed, the following key commands are available: * UP/DOWN: Open the menu, if the minLength has been met. THEMING The autocomplete widget uses the jQuery UI CSS framework to style its look and feel. If autocomplete specific styling is needed, the following CSS class names can be used for overrides or as keys for the classes option: * ui-autocomplete: The menu used to display matches to the user. * ui-autocomplete-input: The input element that the autocomplete widget was instantiated with. While requesting data to display to the user, the ui-autocomplete-loading class is also added to this element. DEPENDENCIES * UI Core * Widget Factory * Position * Menu ADDITIONAL NOTES: * This widget requires some functional CSS, otherwise it won't work. If you build a custom theme, use the widget's specific CSS file as a starting point. * This widget manipulates its element's value programmatically, therefore a native change event may not be fired when the element's value changes. OPTIONS APPENDTO Type: Selector Default: null Which element the menu should be appended to. When the value is null, the parents of the input field will be checked for a class of ui-front. If an element with the ui-front class is found, the menu will be appended to that element. Regardless of the value, if no element is found, the menu will be appended to the body. Note: The appendTo option should not be changed while the suggestions menu is open. Code examples: Initialize the autocomplete with the appendTo option specified: 1 2 3 $( ".selector" ).autocomplete({ appendTo: "#someElem" }); Get or set the appendTo option, after initialization: 1 2 3 4 5 // Getter var appendTo = $( ".selector" ).autocomplete( "option", "appendTo" ); // Setter $( ".selector" ).autocomplete( "option", "appendTo", "#someElem" ); AUTOFOCUS Type: Boolean Default: false If set to true the first item will automatically be focused when the menu is shown. Code examples: Initialize the autocomplete with the autoFocus option specified: 1 2 3 $( ".selector" ).autocomplete({ autoFocus: true }); Get or set the autoFocus option, after initialization: 1 2 3 4 5 // Getter var autoFocus = $( ".selector" ).autocomplete( "option", "autoFocus" ); // Setter $( ".selector" ).autocomplete( "option", "autoFocus", true ); CLASSES Type: Object Default: {} Specify additional classes to add to the widget's elements. Any of classes specified in the Theming section can be used as keys to override their value. To learn more about this option, check out the learn article about the classes option. Code examples: Initialize the autocomplete with the classes option specified, changing the theming for the ui-autocomplete class: 1 2 3 4 5 $( ".selector" ).autocomplete({ classes: { "ui-autocomplete": "highlight" } }); Get or set a property of the classes option, after initialization, here reading and changing the theming for the ui-autocomplete class: 1 2 3 4 5 // Getter var themeClass = $( ".selector" ).autocomplete( "option", "classes.ui-autocomplete" ); // Setter $( ".selector" ).autocomplete( "option", "classes.ui-autocomplete", "highlight" ); DELAY Type: Integer Default: 300 The delay in milliseconds between when a keystroke occurs and when a search is performed. A zero-delay makes sense for local data (more responsive), but can produce a lot of load for remote data, while being less responsive. Code examples: Initialize the autocomplete with the delay option specified: 1 2 3 $( ".selector" ).autocomplete({ delay: 500 }); Get or set the delay option, after initialization: 1 2 3 4 5 // Getter var delay = $( ".selector" ).autocomplete( "option", "delay" ); // Setter $( ".selector" ).autocomplete( "option", "delay", 500 ); DISABLED Type: Boolean Default: false Disables the autocomplete if set to true. Code examples: Initialize the autocomplete with the disabled option specified: 1 2 3 $( ".selector" ).autocomplete({ disabled: true }); Get or set the disabled option, after initialization: 1 2 3 4 5 // Getter var disabled = $( ".selector" ).autocomplete( "option", "disabled" ); // Setter $( ".selector" ).autocomplete( "option", "disabled", true ); MINLENGTH Type: Integer Default: 1 The minimum number of characters a user must type before a search is performed. Zero is useful for local data with just a few items, but a higher value should be used when a single character search could match a few thousand items. Code examples: Initialize the autocomplete with the minLength option specified: 1 2 3 $( ".selector" ).autocomplete({ minLength: 0 }); Get or set the minLength option, after initialization: 1 2 3 4 5 // Getter var minLength = $( ".selector" ).autocomplete( "option", "minLength" ); // Setter $( ".selector" ).autocomplete( "option", "minLength", 0 ); POSITION Type: Object Default: { my: "left top", at: "left bottom", collision: "none" } Identifies the position of the suggestions menu in relation to the associated input element. The of option defaults to the input element, but you can specify another element to position against. You can refer to the jQuery UI Position utility for more details about the various options. Code examples: Initialize the autocomplete with the position option specified: 1 2 3 $( ".selector" ).autocomplete({ position: { my : "right top", at: "right bottom" } }); Get or set the position option, after initialization: 1 2 3 4 5 // Getter var position = $( ".selector" ).autocomplete( "option", "position" ); // Setter $( ".selector" ).autocomplete( "option", "position", { my : "right top", at: "right bottom" } ); SOURCE Type: Array or String or Function( Object request, Function response( Object data ) ) Default: none; must be specified Defines the data to use, must be specified. Independent of the variant you use, the label is always treated as text. If you want the label to be treated as html you can use Scott González' html extension. The demos all focus on different variations of the source option - look for one that matches your use case, and check out the code. Multiple types supported: * Array: An array can be used for local data. There are two supported formats: * An array of strings: [ "Choice1", "Choice2" ] * An array of objects with label and value properties: [ { label: "Choice1", value: "value1" }, ... ] The label property is displayed in the suggestion menu. The value will be inserted into the input element when a user selects an item. If just one property is specified, it will be used for both, e.g., if you provide only value properties, the value will also be used as the label. * String: When a string is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data. It can be on the same host or on a different one (must support CORS). The Autocomplete plugin does not filter the results, instead a query string is added with a term field, which the server-side script should use for filtering the results. For example, if the source option is set to "https://example.com" and the user types foo, a GET request would be made to https://example.com?term=foo. The data itself can be in the same format as the local data described above. * Function: The third variation, a callback, provides the most flexibility and can be used to connect any data source to Autocomplete, including JSONP. The callback gets two arguments: * A request object, with a single term property, which refers to the value currently in the text input. For example, if the user enters "new yo" in a city field, the Autocomplete term will equal "new yo". * A response callback, which expects a single argument: the data to suggest to the user. This data should be filtered based on the provided term, and can be in any of the formats described above for simple local data. It's important when providing a custom source callback to handle errors during the request. You must always call the response callback even if you encounter an error. This ensures that the widget always has the correct state. When filtering data locally, you can make use of the built-in $.ui.autocomplete.escapeRegex function. It'll take a single string argument and escape all regex characters, making the result safe to pass to new RegExp(). Code examples: Initialize the autocomplete with the source option specified: 1 2 3 $( ".selector" ).autocomplete({ source: [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ] }); Get or set the source option, after initialization: 1 2 3 4 5 // Getter var source = $( ".selector" ).autocomplete( "option", "source" ); // Setter $( ".selector" ).autocomplete( "option", "source", [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ] ); METHODS CLOSE()RETURNS: JQUERY (PLUGIN ONLY) Closes the Autocomplete menu. Useful in combination with the search method, to close the open menu. * This method does not accept any arguments. Code examples: Invoke the close method: 1 $( ".selector" ).autocomplete( "close" ); DESTROY()RETURNS: JQUERY (PLUGIN ONLY) Removes the autocomplete functionality completely. This will return the element back to its pre-init state. * This method does not accept any arguments. Code examples: Invoke the destroy method: 1 $( ".selector" ).autocomplete( "destroy" ); DISABLE()RETURNS: JQUERY (PLUGIN ONLY) Disables the autocomplete. * This method does not accept any arguments. Code examples: Invoke the disable method: 1 $( ".selector" ).autocomplete( "disable" ); ENABLE()RETURNS: JQUERY (PLUGIN ONLY) Enables the autocomplete. * This method does not accept any arguments. Code examples: Invoke the enable method: 1 $( ".selector" ).autocomplete( "enable" ); INSTANCE()RETURNS: OBJECT Retrieves the autocomplete's instance object. If the element does not have an associated instance, undefined is returned. Unlike other widget methods, instance() is safe to call on any element after the autocomplete plugin has loaded. * This method does not accept any arguments. Code examples: Invoke the instance method: 1 $( ".selector" ).autocomplete( "instance" ); OPTION( OPTIONNAME )RETURNS: OBJECT Gets the value currently associated with the specified optionName. Note: For options that have objects as their value, you can get the value of a specific key by using dot notation. For example, "foo.bar" would get the value of the bar property on the foo option. * optionName Type: String The name of the option to get. Code examples: Invoke the method: 1 var isDisabled = $( ".selector" ).autocomplete( "option", "disabled" ); OPTION()RETURNS: PLAINOBJECT Gets an object containing key/value pairs representing the current autocomplete options hash. * This signature does not accept any arguments. Code examples: Invoke the method: 1 var options = $( ".selector" ).autocomplete( "option" ); OPTION( OPTIONNAME, VALUE )RETURNS: JQUERY (PLUGIN ONLY) Sets the value of the autocomplete option associated with the specified optionName. Note: For options that have objects as their value, you can set the value of just one property by using dot notation for optionName. For example, "foo.bar" would update only the bar property of the foo option. * optionName Type: String The name of the option to set. * value Type: Object A value to set for the option. Code examples: Invoke the method: 1 $( ".selector" ).autocomplete( "option", "disabled", true ); OPTION( OPTIONS )RETURNS: JQUERY (PLUGIN ONLY) Sets one or more options for the autocomplete. * options Type: Object A map of option-value pairs to set. Code examples: Invoke the method: 1 $( ".selector" ).autocomplete( "option", { disabled: true } ); SEARCH( [VALUE ] )RETURNS: JQUERY (PLUGIN ONLY) Triggers a search event and invokes the data source if the event is not canceled. Can be used by a selectbox-like button to open the suggestions when clicked. When invoked with no parameters, the current input's value is used. Can be called with an empty string and minLength: 0 to display all items. * value Type: String Code examples: Invoke the search method: 1 $( ".selector" ).autocomplete( "search", "" ); WIDGET()RETURNS: JQUERY Returns a jQuery object containing the menu element. Although the menu items are constantly created and destroyed, the menu element itself is created during initialization and is constantly reused. * This method does not accept any arguments. Code examples: Invoke the widget method: 1 $( ".selector" ).autocomplete( "widget" ); EXTENSION POINTS The autocomplete widget is built with the widget factory and can be extended. When extending widgets, you have the ability to override or add to the behavior of existing methods. The following methods are provided as extension points with the same API stability as the plugin methods listed above. For more information on widget extensions, see Extending Widgets with the Widget Factory. -------------------------------------------------------------------------------- _RENDERITEM( UL, ITEM )RETURNS: JQUERY Method that controls the creation of each option in the widget's menu. The method must create a new <li> element, append it to the menu, and return it. See the Menu documentation for more details about the markup. * ul Type: jQuery The <ul> element that the newly created <li> element must be appended to. * item Type: Object * label Type: String The string to display for the item. * value Type: String The value to insert into the input when the item is selected. Code examples: Add the item's value as a data attribute on the <li>. 1 2 3 4 5 6 _renderItem: function( ul, item ) { return $( "<li>" ) .attr( "data-value", item.value ) .append( item.label ) .appendTo( ul ); } _RENDERMENU( UL, ITEMS )RETURNS: JQUERY (PLUGIN ONLY) Method that controls building the widget's menu. The method is passed an empty <ul> and an array of items that match the user typed term. Creation of the individual <li> elements should be delegated to _renderItemData(), which in turn delegates to the _renderItem() extension point. * ul Type: jQuery An empty <ul> element to use as the widget's menu. * items Type: Array An Array of items that match the user typed term. Each item is an Object with label and value properties. Code examples: Add a CSS class name to the odd menu items. 1 2 3 4 5 6 7 _renderMenu: function( ul, items ) { var that = this; $.each( items, function( index, item ) { that._renderItemData( ul, item ); }); $( ul ).find( "li" ).odd().addClass( "odd" ); } _RESIZEMENU()RETURNS: JQUERY (PLUGIN ONLY) Method responsible for sizing the menu before it is displayed. The menu element is available at this.menu.element. * This method does not accept any arguments. Code examples: Always display the menu as 500 pixels wide. 1 2 3 _resizeMenu: function() { this.menu.element.outerWidth( 500 ); } EVENTS CHANGE( EVENT, UI )TYPE: AUTOCOMPLETECHANGE Triggered when the field is blurred, if the value has changed. * event Type: Event * ui Type: Object * item Type: Object The item selected from the menu, if any. Otherwise the property is null. Code examples: Initialize the autocomplete with the change callback specified: 1 2 3 $( ".selector" ).autocomplete({ change: function( event, ui ) {} }); Bind an event listener to the autocompletechange event: 1 $( ".selector" ).on( "autocompletechange", function( event, ui ) {} ); CLOSE( EVENT, UI )TYPE: AUTOCOMPLETECLOSE Triggered when the menu is hidden. Not every close event will be accompanied by a change event. * event Type: Event * ui Type: Object Note: The ui object is empty but included for consistency with other events. Code examples: Initialize the autocomplete with the close callback specified: 1 2 3 $( ".selector" ).autocomplete({ close: function( event, ui ) {} }); Bind an event listener to the autocompleteclose event: 1 $( ".selector" ).on( "autocompleteclose", function( event, ui ) {} ); CREATE( EVENT, UI )TYPE: AUTOCOMPLETECREATE Triggered when the autocomplete is created. * event Type: Event * ui Type: Object Note: The ui object is empty but included for consistency with other events. Code examples: Initialize the autocomplete with the create callback specified: 1 2 3 $( ".selector" ).autocomplete({ create: function( event, ui ) {} }); Bind an event listener to the autocompletecreate event: 1 $( ".selector" ).on( "autocompletecreate", function( event, ui ) {} ); FOCUS( EVENT, UI )TYPE: AUTOCOMPLETEFOCUS Triggered when focus is moved to an item (not selecting). The default action is to replace the text field's value with the value of the focused item, though only if the event was triggered by a keyboard interaction. Canceling this event prevents the value from being updated, but does not prevent the menu item from being focused. * event Type: Event * ui Type: Object * item Type: Object The focused item. Code examples: Initialize the autocomplete with the focus callback specified: 1 2 3 $( ".selector" ).autocomplete({ focus: function( event, ui ) {} }); Bind an event listener to the autocompletefocus event: 1 $( ".selector" ).on( "autocompletefocus", function( event, ui ) {} ); OPEN( EVENT, UI )TYPE: AUTOCOMPLETEOPEN Triggered when the suggestion menu is opened or updated. * event Type: Event * ui Type: Object Note: The ui object is empty but included for consistency with other events. Code examples: Initialize the autocomplete with the open callback specified: 1 2 3 $( ".selector" ).autocomplete({ open: function( event, ui ) {} }); Bind an event listener to the autocompleteopen event: 1 $( ".selector" ).on( "autocompleteopen", function( event, ui ) {} ); RESPONSE( EVENT, UI )TYPE: AUTOCOMPLETERESPONSE Triggered after a search completes, before the menu is shown. Useful for local manipulation of suggestion data, where a custom source option callback is not required. This event is always triggered when a search completes, even if the menu will not be shown because there are no results or the Autocomplete is disabled. * event Type: Event * ui Type: Object * content Type: Array Contains the response data and can be modified to change the results that will be shown. This data is already normalized, so if you modify the data, make sure to include both value and label properties for each item. Code examples: Initialize the autocomplete with the response callback specified: 1 2 3 $( ".selector" ).autocomplete({ response: function( event, ui ) {} }); Bind an event listener to the autocompleteresponse event: 1 $( ".selector" ).on( "autocompleteresponse", function( event, ui ) {} ); SEARCH( EVENT, UI )TYPE: AUTOCOMPLETESEARCH Triggered before a search is performed, after minLength and delay are met. If canceled, then no request will be started and no items suggested. * event Type: Event * ui Type: Object Note: The ui object is empty but included for consistency with other events. Code examples: Initialize the autocomplete with the search callback specified: 1 2 3 $( ".selector" ).autocomplete({ search: function( event, ui ) {} }); Bind an event listener to the autocompletesearch event: 1 $( ".selector" ).on( "autocompletesearch", function( event, ui ) {} ); SELECT( EVENT, UI )TYPE: AUTOCOMPLETESELECT Triggered when an item is selected from the menu. The default action is to replace the text field's value with the value of the selected item. Canceling this event prevents the value from being updated, but does not prevent the menu from closing. * event Type: Event * ui Type: Object * item Type: Object An Object with label and value properties for the selected option. Code examples: Initialize the autocomplete with the select callback specified: 1 2 3 $( ".selector" ).autocomplete({ select: function( event, ui ) {} }); Bind an event listener to the autocompleteselect event: 1 $( ".selector" ).on( "autocompleteselect", function( event, ui ) {} ); EXAMPLES: A simple jQuery UI Autocomplete 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>autocomplete demo</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.12.4.js"></script> <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <label for="autocomplete">Select a programming language: </label> <input id="autocomplete"> <script> $( "#autocomplete" ).autocomplete({ source: [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ] }); </script> </body> </html> DEMO: Using a custom source callback to match only the beginning of terms 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>autocomplete demo</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.12.4.js"></script> <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <label for="autocomplete">Select a programming language: </label> <input id="autocomplete"> <script> var tags = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ]; $( "#autocomplete" ).autocomplete({ source: function( request, response ) { var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" ); response( $.grep( tags, function( item ){ return matcher.test( item ); }) ); } }); </script> </body> </html> DEMO: JQUERY UI 1.13 * Effects * Effects Core * Interactions * Method Overrides * Methods * Selectors * Theming * UI Core * Utilities * Widgets OTHER VERSIONS * jQuery UI 1.12 * jQuery UI 1.11 * jQuery UI 1.10 * jQuery UI 1.9 * jQuery UI 1.8 BOOKS * jQuery UI in Action TJ VanToll * jQuery UI Themes Adam Boduch * jQuery UI Cookbook Adam Boduch * Learning Center * Forum * Twitter * IRC * GitHub Copyright 2024 OpenJS Foundation and jQuery contributors. All rights reserved. See jQuery License for more information. The OpenJS Foundation has registered trademarks and uses trademarks. For a list of trademarks of the OpenJS Foundation, please see our Trademark Policy and Trademark List. Trademarks and logos not indicated on the list of OpenJS Foundation trademarks are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them. OpenJS Foundation Terms of Use, Privacy, and Cookie Policies also apply. Web hosting by Digital Ocean | CDN by Fastly | Powered by WordPress