JSFiddle - React, Tailwind, and code Playground

by kentaromiura

JavaScript

var build = function(json) {
    var i;
    for (var combo in json) {
        var current = json[combo];
        var select = new Element('select', {
            id: combo
        });
        select.inject(document.documentElement);
        
        if (current.depend) {
            $(current.depend).addEvent('change', function(e){
                var id = e.target.value;
                select.empty();
                for (i = 0, max = current.values[id].length; i < max; i++) {
                   
                    new Element('option', {
                        text: current.values[id][i],
                        value: i
                    }).inject(select);
                }    
            })
        } else {
            for (i = 0, max = current.values.length; i < max; i++) {
                
                new Element('option', {
                    text: current.values[i],
                    value: i
                }).inject(select);
            }
        }
    }
};

build({
    "house": {
        
        values: ['ONE', 'TWO']
    },
    "foo": {
        
        depend: 'house',
        values: [
            ['FROM ONE 1', 'FROM ONE 2'],
            ['FROM TWO 1', 'FROM TWO 2']
            ]
    }
});