Henry Taylor
Henry Taylor

Reputation: 91

Populating Select

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?

Upvotes: 0

Views: 91

Answers (1)

Francis
Francis

Reputation: 3383

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/

Upvotes: 2

Related Questions