Skip to content

Instantly share code, notes, and snippets.

@jaimeiniesta
Created August 12, 2011 15:41

Revisions

  1. jaimeiniesta created this gist Aug 12, 2011.
    32 changes: 32 additions & 0 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    $(function() {
    function split( val ) {
    return val.split( /,\s*/ );
    }

    function extractLast( term ) {
    return split( term ).pop();
    }

    $( "#new_interest" ).autocomplete({
    source: function( request, response ) {
    $.getJSON( "/tags.json", {
    term: extractLast( request.term )
    }, response );
    },
    select: function( event, ui ) {
    // Add the selected term appending to the current values with a comma
    var terms = split( this.value );
    // remove the current input
    terms.pop();
    // add the selected item
    terms.push( ui.item.value );
    // join all terms with a comma
    this.value = terms.join( ", " );
    return false;
    },
    focus: function() {
    // prevent value inserted on focus when navigating the drop down list
    return false;
    }
    });
    });
    13 changes: 13 additions & 0 deletions tags.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    [{
    "id": 28,
    "name": "sex",
    "value": "sex"
    }, {
    "id": 29,
    "name": "drugs",
    "value": "drugs"
    }, {
    "id": 30,
    "name": "rock and roll",
    "value": "rock and roll"
    }]