Last active
August 29, 2015 14:01
-
-
Save securingsincity/5cd6ad1bfd22c9ea100e to your computer and use it in GitHub Desktop.
Spotify multiple input jquery-ui autocomplete
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function split( val ) { | |
return val.split( /;\s*/ ); | |
} | |
function extractLast( term ) { | |
return split( term ).pop(); | |
} | |
$("#spotify_song_search").bind( "keydown", function( event ) { | |
if ( event.keyCode === $.ui.keyCode.TAB && $( this ).data( "ui-autocomplete" ).menu.active ) { | |
event.preventDefault(); | |
} | |
}).autocomplete({ | |
source: function(request, response) { | |
$.get("http://ws.spotify.com/search/1/track.json", { | |
q: extractLast( request.term ) | |
}, function(data) { | |
response($.map(data.tracks, function(item) { | |
return {label: item.name + " by " + item.artists[0].name, track: item}; | |
})); | |
}); | |
}, | |
focus: function() { | |
// prevent value inserted on focus | |
return false; | |
}, | |
select: function( event, ui ) { | |
var terms = split( this.value ); | |
// remove the current input | |
terms.pop(); | |
// add the selected item | |
terms.push( ui.item.value ); | |
// add placeholder to get the comma-and-space at the end | |
terms.push( "" ); | |
this.value = terms.join( "; " ); | |
return false; | |
}, | |
minLength: 3, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment