0

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>
  • How do you receive a request? Is it AJAX or what? – Ivan Apr 28 '13 at 00:29
  • @Ivan I do not know exactly haha, I started this project from this: https://github.com/fxb/javascript-last.fm-api. But I'm not having problems with the request, but as automatically put him in the field. Thanks. – Rômulo Argolo Apr 28 '13 at 00:38
  • @RomuloArgolo I'm just syaing that if it is used with AJAX then you can place inside success: `function() {}` some code that will put your data into fields you want – Ivan Apr 28 '13 at 00:44

1 Answers1

0

I've have put the code that you have from GitHub into a fiddle, but I don't know anything about the API to say what needs to happen next.

Changes I made to fix immediate error that was shown in console.log

HTML

<form action="" method="post">
    <input name="bandname" id="bandname" value="">
    <input name="forfansof" id="forfans" style="width:300px;" value="">
</form>

JavaScript

document.getElementById("bandname").addEventListener("keypess", getName, false);

On jsfiddle

Xotic750
  • 22,914
  • 8
  • 57
  • 79