开发者

Cannot access variable through JavaScript - scope error?

开发者 https://www.devze.com 2023-04-07 14:38 出处:网络
I have some data in a separate .js file similar to this: data = new Object(); data[\'cat\'] = [\'Mr. Whiskers\',\'Wobbles\'];

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 stuf开发者_开发百科f = 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?


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));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号