I'm new to Javascript so this may not be the best approach. I'm doing Bellycard's web app challenge #1 for fun.
I query their search endpoint, which returns JSON, something like this: https://rdio-service.herokuapp.com/search?q=korn (click it)
I get the unique search type:
//artist, album, tracks
var searchTypes = $.unique(data.data.map(function(d) {return d.type}));
Then I iterate searchTypes and filter the original data JSON for that searchType. I learned how to .appendChild to an existing item on the GUI just fine. But I don't know how to display the results under each searchType. Code below.
//iterate searchTypes and display them foreach searchType
for(var i = 0; i < searchTypes.length; i++)
{
var searchType = searchTypes[i];
var newDiv = document.createElement('div');
newDiv.id = searchType + "Result";
//and then, each search type should have results for their type
//select specific type for this iteration and put it in a results array
var resultsThisType = data.data.filter(function (f) {
return f.type == searchType;
});
for(var j = 0; j < resultsThisType.length; j++) {
var newP = document.createElement('p'); //put it in a 'p' item for now, for testing
newP.id = searchType + i + j;
newP.innerHTML = resultsThisType[i].name; //test
document.getElementById(searchType + "Result").appendChild(newP); //error here... what's the right approach?
}
newDiv.className = "typeResult";
newDiv.innerHTML = "<h2>" + searchType + "</h2>";
document.getElementById("results").appendChild(newDiv);
}
document.getElementById(searchType + "Result")is null and no such method as .appendChild.