Skip to content

Instantly share code, notes, and snippets.

@shushugah
Created Aug 14, 2016
Embed
What would you like to do?
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