Skip to content

Instantly share code, notes, and snippets.

@kristian76
Created April 5, 2018 11:55
Show Gist options
  • Save kristian76/e365f29a1653d65868236c74c9a2caef to your computer and use it in GitHub Desktop.
Save kristian76/e365f29a1653d65868236c74c9a2caef to your computer and use it in GitHub Desktop.
Populate drop down
<html>
<form>
<select id="parent">
<option value="set1">Set opt1</option>
<option value="set2">Set opt2</option>
</select>
<select id="child">
</select>
</form>
<script>
var set1 = [1,2,3,4,5],
set2 = [6,7,8,9],
child = document.querySelector('#child');
document.querySelector('#parent').onchange = function(event) {
var selIndex = event.target.selectedIndex,
data = [];
if (selIndex == 0) {
data = set1;
} else {
data = set2;
}
while (child.options.length > 0) {
child.remove(0);
}
for (var i in data) {
var opt = document.createElement('option');
opt.value = i;
opt.text = data[i];
child.add(opt);
}
};
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment