5 <title>jQuery UI Autocomplete - Multiple values</title>
6 <link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
7 <script src="../../jquery-1.9.1.js"></script>
8 <script src="../../ui/jquery.ui.core.js"></script>
9 <script src="../../ui/jquery.ui.widget.js"></script>
10 <script src="../../ui/jquery.ui.position.js"></script>
11 <script src="../../ui/jquery.ui.menu.js"></script>
12 <script src="../../ui/jquery.ui.autocomplete.js"></script>
13 <link rel="stylesheet" href="../demos.css">
40 function split( val ) {
41 return val.split( /,\s*/ );
43 function extractLast( term ) {
44 return split( term ).pop();
48 // don't navigate away from the field on tab when selecting an item
49 .bind( "keydown", function( event ) {
50 if ( event.keyCode === $.ui.keyCode.TAB &&
51 $( this ).data( "ui-autocomplete" ).menu.active ) {
52 event.preventDefault();
57 source: function( request, response ) {
58 // delegate back to autocomplete, but extract the last term
59 response( $.ui.autocomplete.filter(
60 availableTags, extractLast( request.term ) ) );
63 // prevent value inserted on focus
66 select: function( event, ui ) {
67 var terms = split( this.value );
68 // remove the current input
70 // add the selected item
71 terms.push( ui.item.value );
72 // add placeholder to get the comma-and-space at the end
74 this.value = terms.join( ", " );
83 <div class="ui-widget">
84 <label for="tags">Tag programming languages: </label>
85 <input id="tags" size="50">
88 <div class="demo-description">
89 <p>Usage: Type something, eg. "j" to see suggestions for tagging with programming languages. Select a value, then continue typing to add more.</p>
90 <p>This is an example showing how to use the source-option along with some events to enable autocompleting multiple values into a single field.</p>