Naoto
Naoto

Reputation: 1

Cannot access variable through JavaScript - scope error?

I have some data in a separate .js file similar to this:

data = new Object();
data['cat'] = ['Mr. Whiskers','Wobbles'];
data['dog'] = ['Toothy'];
data['fish'] = ['goldy','roose'];

function getStuff(info)
{
  var stuff = data[info.value];
  return stuff;
}

Now in another html file with a block, I have something like this:

function theDrop(dynamic) {
  alert(getStuff(dynamic));
}

The box says undefined, why?

Upvotes: 0

Views: 71

Answers (1)

Loktar
Loktar

Reputation: 35309

What are you passing to theDrop? If you want to call the .value then you need to pass the whole object over otherwise you will get undefined

Live Demo

var select = document.getElementById("selectme");

select.onchange = function(){
    theDrop(this);
}

data = new Object();
data['cat'] = ['Mr. Whiskers','Wobbles'];
data['dog'] = ['Toothy'];
data['fish'] = ['goldy','roose'];

function getStuff(info)
{
    var stuff = data[info.value];
    return stuff;
}

function theDrop(dynamic) {
    alert(getStuff(dynamic));
}

Upvotes: 3

Related Questions