Skip to content

Instantly share code, notes, and snippets.

@shushugah
Created August 14, 2016 11:54
Show Gist options
  • Save shushugah/35e8f49af59bfee7cf94f520589cb390 to your computer and use it in GitHub Desktop.
Save shushugah/35e8f49af59bfee7cf94f520589cb390 to your computer and use it in GitHub Desktop.
var submit = $("button");
var inputField = $("input");
var menuType = $("select");
var listContainer = $("#listContainer");
var selectType;
var results;
submit.on('click', function(){
var artistName = inputField.val();
selectType = menuType.val();
$.ajax({
url: "https://api.spotify.com/v1/search",
data: {
q: artistName,
type: selectType
},
success: insertHTML
});
});
function insertHTML(artistInfo){
var items
if (selectType == "artist"){
items = artistInfo.artists.items;
}
if (selectType == "album"){
items = artistInfo.albums.items;
}
for (var i = 0; i < items.length; i++){
var artistName = items[i].name;
var images = items[i].images;
var outsideURL = items[i].external_urls.spotify;
if (images.length >= 3){
var imageURL = items[i].images[2].url;
var results = '<h2>Results for ' + inputField.val() + '</h2>';
listContainer.append('<div class="results"> <img src=' + imageURL + ' ">' + '<a href="' + outsideURL + '" class="title">' + artistName + '</a></div>');
}
}
listContainer.prepend(results);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment