I'm new to javascript, I'm working on a project that will using and enjoying the API of Last.fm.
But my question is the following: I will make 2 fields:
First: Band name
Second: For fans of
When entering the name of the band I'll be sending a request to the last.fm API, but even there I am doing well.
The problem is that when I receive the request I want it automatically puts in the "For fans of" field.
I tried using the events onkeypress, onkeydown .. but have not had much success.
Anyone have any suggestions?
The request is made through these files: github.com/fxb/javascript-last.fm-api
My code is as follows:
Javascript:
/* Create a cache object */
var cache = new LastFMCache();
/* Create a LastFM object */
var lastfm = new LastFM(
{
apiKey : 'XXXXX',
apiSecret : 'XXXXXX',
cache : cache
});
function getName()
{
var bandname = $('#bandname').val();
var forfansof = $('#forfans').val();
forfansof = bandname;
/* Load some artist info. */
lastfm.artist.getSimilar(
{
artist: bandname
},
{
success: function(data)
{
/* Pega os 3 nomes das bandas similares */
for(var i = 0 ; i < 3 ; i++)
{
var artist = data.similarartists.artist[i].name;
$('#forfans').val($('#forfans').val() + artist + ", ");
}
},
error: function(code, message)
{
$('#forfans').val("Nenhum artista encontrado.");
}
});
}
HTML:
<form action="" method="post">
<input name="bandname" id="bandname" value="" onkeypress="javascript:getName();">
<input name="forfansof" id="forfans" style="width:300px;" value="">
</form>