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 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));
}
加载中,请稍侯......
精彩评论