JSFiddle - React, Tailwind, and code Playground
HTML
<select id="box1"></select>
<select id="box2"></select>
<select id="box3"></select>
JavaScript
var box1 = document.getElementById('box1');
var box2 = document.getElementById('box2');
var box3 = document.getElementById('box3');
var options = {
'a': {
'ap': [
'apple',
'apricot',
'apathy',
'apartheid'
],
'al': [
'alligator',
'allegory',
'algorithms'
]
},
'b': {
'ba': [
'banana',
'bartender',
'baccilae'
],
'bu': [
'bumblebee',
'burmese',
'burrowing'
]
}
};
Object.keys(options).forEach(function(x) {
var option = document.createElement('option');
option.value = x;
option.appendChild(document.createTextNode(x));
box1.appendChild(option);
});
box1.onchange = function() {
while(box2.childNodes.length > 0) box2.removeChild(box2.lastChild);
Object.keys(options[box1.value]).forEach(function(x) {
var option = document.createElement('option');
option.value = x;
option.appendChild(document.createTextNode(x));
box2.appendChild(option);
});
box2.onchange();
};
box2.onchange = function() {
while(box3.childNodes.length > 0) box3.removeChild(box3.lastChild);
options[box1.value][box2.value].forEach(function(x) {
var option = document.createElement('option');
option.value = x;
option.appendChild(document.createTextNode(x));
box3.appendChild(option);
});
};
box1.onchange();