0

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?

12
  • Your syntax is invalid - alert(getStuff(dynamic)); Commented Sep 27, 2011 at 2:21
  • I realized that and just fixed it. This is just sample code that I wrote, and is not my actual code - it's for a class and I'm not supposed to share it outright. It alerts fine, but alerts as undefined, not as it should. Commented Sep 27, 2011 at 2:21
  • how is theDrop being called? Commented Sep 27, 2011 at 2:21
  • 1
    It is called as an onChange method when a drop down is changed. Commented Sep 27, 2011 at 2:23
  • I'm assuming the other js file is included in the html file with theDrop ? Commented Sep 27, 2011 at 2:23

1 Answer 1

3

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));
}
Sign up to request clarification or add additional context in comments.

2 Comments

It turns out, you were correct fundamentally - I was indeed passing the .value and not the object itself. Alerting dynamic.value gets what I needed. Thanks so much!
@Naoto If this answers your question you might consider accepting this as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.