0

I created a webpage where in a select element (Object) will populate based on 2 other select element (Type1,Type2). To do this, I created an array like this:

"Animal:Fish:Tuna", "Animal:Fish:Salmon", "Animal:Mammal:Dog", "Animal:Mammal:Cat", "Object:Metal:Aluminum", "Object:Metal:Mercury"

Then each of these elements will be separated by ':'. The first two strings will be compared to Type1 and Type2. Then the third element will be populated in Object.

I there any other easy way of doing this in js?

  • Take a look here: http://stackoverflow.com/questions/5861090/populating-one-select-box-based-on-the-selection-in-another-select-box-jquery – Walter81 Jun 15 '12 at 12:53

1 Answers1

2

Instead of an array, you can use an object containing your relationship

var data = {
    Animal: {
        Fish: ['Tuna', 'Salmon'],
        Mammal: ['Dog', 'Cat']
    },
    Object: {
        Metal: ['Aluminum', 'Mercury']
    }
};

Live demo: http://jsfiddle.net/u53Mt/

Francis
  • 3,335
  • 20
  • 46