1

I seem to have a problem with Javascript. I am currently trying to use arrays to populate dropdown menus. They are dynamically appearing drop down menus. In my example, the countries and cities and activites are populating correctly for USA. However, for Canada and UK, the activities list is still using the USA's activities list. Can someone tell me what I'm doing wrong? If what I just explained seems a bit confusing, please take a look at the jsfiddle I made here: http://jsfiddle.net/KCz3G/

<title>Page Title</title>
6
  • like, I click on USA, then a city, and the next dropdown menu populates itself with the correct array of activities but Canada and UK's activities list still use USA's activities arrays. Commented Mar 5, 2014 at 5:37
  • can you tell/help me please? I'm really frustrated at this. Commented Mar 5, 2014 at 5:39
  • I kind of get what you mean, so how would I correct it? Commented Mar 5, 2014 at 5:48
  • the problem is that the other functions which make the dropdown menu dynamically appear rely on an array to store the items. Commented Mar 5, 2014 at 5:54
  • so doing what you said would need for me to completely rewrite the code Commented Mar 5, 2014 at 5:59

1 Answer 1

1

If you use this as a start to store the data, things might get easier:

Simple demo: http://jsfiddle.net/KCz3G/3/

var cities = {
 usa: ['New York|newyorkvalue', ...],
 uk: ['London|londonvalue', ...]
};

var activities = {
 newyorkvalue: ['xxx|yyy', ...],
 londonvalue: ['xxx|yyy', ...]
};

//get cities in usa
for (i=0;i<cities["usa"].length;i++) {

}

Which mean you use/pass the "value text", like londonvalue as a parameter instead of an index to find which array of "something" to populate.

It will give you much more flexibility, still using arrays of values.

Sign up to request clarification or add additional context in comments.

12 Comments

will this affect the other functions in the code, particularly updatecities and updateactivites?
hmmm, how would I do that? Sorry about the questions, my mind is really shot right now. And do I need that for loop?
okay, so this is what I have on my jsfiddle now: jsfiddle.net/KCz3G you said to use the text value, by that, you mean take out objIndex?
all I see changed is this value: objIndex = y.options[y.selectedIndex].value;
I am trying to teach/show you how to do it, so you can understand what it does. Anything else will be "doing it for you" which will not help in the long run. Take your time going through yours and mine, step by step, and see what it does, and as soon you see that, it will be a piece of cake to both do it and even optimize it yourself.
|

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.