Expand all

jquery.suggestions

Provides a jQuery plugin that add suggestions to a text box.

Methods

$.fn.suggestions(options) → {jQuery}static #

Add suggestions to a text box.

Set options:

$( '#textbox' ).suggestions( { option1: value1, option2: value2 } );
$( '#textbox' ).suggestions( option, value );

To use this jQuery plugin, load the jquery.suggestions module with mw.loader.

Example

// Initialize:
mw.loader.using( 'jquery.suggestions' ).then(()=> {
 $( '#textbox' ).suggestions();
});

Parameters:

Name Type Description
options Object
Properties:
Name Type Attributes Default Description
fetch function optional

Callback that should fetch suggestions and set the suggestions property. Called in context of the text box.

Properties:
Name Type Description
query string
response function

Callback to receive the suggestions with

Properties:
Name Type Description
suggestions Array
maxRows number
cancel function optional

Callback function to call when any pending asynchronous suggestions fetches. Called in context of the text box.

special Object optional

Set of callbacks for rendering and selecting.

Properties:
Name Type Description
render function

Called in context of the suggestions-special element.

Properties:
Name Type Description
query string
context Object
select function

Called in context of the suggestions-result-current element.

Properties:
Name Type Description
$textbox jQuery
result Object optional

Set of callbacks for rendering and selecting

Properties:
Name Type Description
render function

Called in context of the suggestions-result element.

Properties:
Name Type Description
suggestion string
context Object
select function

Called in context of the suggestions-result-current element.

Properties:
Name Type Description
$textbox jQuery
update Object optional

Set of callbacks for listening to a change in the text input.

Properties:
Name Type Description
before function

Called right after the user changes the textbox text.

after function

Called after results are updated either from the cache or the API as a result of the user input.

$region jQuery optional
this

The element to place the suggestions below and match width of.

suggestions Array.<string> optional

Array of suggestions to display.

maxRows number optional
10

Maximum number of suggestions to display at one time. Must be between 1 and 100.

delay number optional
120

Number of milliseconds to wait for the user to stop typing. Must be between 0 and 1200.

cache boolean optional
false

Whether to cache results from a fetch.

cacheMaxAge number optional
60000

Number of milliseconds to cache results from a fetch. Must be higher than 1. Defaults to 1 minute.

submitOnClick boolean optional
false

Whether to submit the form containing the textbox when a suggestion is clicked.

maxExpandFactor number optional
3

Maximum suggestions box width relative to the textbox width. If set to e.g. 2, the suggestions box will never be grown beyond 2 times the width of the textbox. Must be higher than 1.

expandFrom string optional
auto

Which direction to offset the suggestion box from. Values 'start' and 'end' translate to left and right respectively depending on the directionality of the current document, according to $( document.documentElement ).css( 'direction' ). Valid values: "left", "right", "start", "end", and "auto".

positionFromLeft boolean optional

Sets expandFrom=left, for backwards compatibility.

highlightInput boolean optional
false

Whether to highlight matched portions of the input or not.

Source:

Returns:

Type
jQuery
Add suggestions to a text box.