I want to see if there's a better way to write this code, solely in Javascript.
There are two kinds of sets, the first set (C7, T7, C11, T11) have four options in the first select, and then up to two options in the second set.
The second set (14X, 17X) have eight options in the first select, and then up to four options in the second set.
Here's the script I've currently got, but it's quite repetitive and long, seems like there could be a better way. Here's the JSFiddle: https://jsfiddle.net/j1fsL9wn/
//Set C7
document.addEventListener("DOMContentLoaded", e => {
'use strict';
const combo = document.getElementById("C7-Drawer-Shelf-Combo"),
bench = document.getElementById("C7-Under-Drawer-Bench");
combo.addEventListener("input", e => {
for (let i = 0; i < bench.children.length; i++)
bench.children[i].hidden = e.target.selectedIndex < 2 && i > e.target.selectedIndex;
if (e.target.selectedIndex && e.target.selectedIndex < 2 && bench.selectedIndex > e.target.selectedIndex)
bench.selectedIndex = 0;
});
});
// Set T7
document.addEventListener("DOMContentLoaded", e => {
'use strict';
const combo = document.getElementById("T7-Drawer-Shelf-Combo"),
bench = document.getElementById("T7-Under-Drawer-Bench");
combo.addEventListener("input", e => {
for (let i = 0; i < bench.children.length; i++)
bench.children[i].hidden = e.target.selectedIndex < 2 && i > e.target.selectedIndex;
if (e.target.selectedIndex && e.target.selectedIndex < 2 && bench.selectedIndex > e.target.selectedIndex)
bench.selectedIndex = 0;
});
});
// Set C11
document.addEventListener("DOMContentLoaded", e => {
'use strict';
const combo = document.getElementById("C11-Drawer-Shelf-Combo"),
bench = document.getElementById("C11-Under-Drawer-Bench");
combo.addEventListener("input", e => {
for (let i = 0; i < bench.children.length; i++)
bench.children[i].hidden = e.target.selectedIndex < 2 && i > e.target.selectedIndex;
if (e.target.selectedIndex && e.target.selectedIndex < 2 && bench.selectedIndex > e.target.selectedIndex)
bench.selectedIndex = 0;
});
});
// Set T11
document.addEventListener("DOMContentLoaded", e => {
'use strict';
const combo = document.getElementById("T11-Drawer-Shelf-Combo"),
bench = document.getElementById("T11-Under-Drawer-Bench");
combo.addEventListener("input", e => {
for (let i = 0; i < bench.children.length; i++)
bench.children[i].hidden = e.target.selectedIndex < 2 && i > e.target.selectedIndex;
if (e.target.selectedIndex && e.target.selectedIndex < 2 && bench.selectedIndex > e.target.selectedIndex)
bench.selectedIndex = 0;
});
});
// Set 14X
document.addEventListener("DOMContentLoaded", e => {
'use strict';
const combo = document.getElementById("14X-Drawer-Shelf-Combo"),
bench = document.getElementById("14X-Under-Drawer-Bench");
combo.addEventListener("input", e => {
for (let i = 0; i < bench.children.length; i++)
bench.children[i].hidden = e.target.selectedIndex < 4 && i > e.target.selectedIndex;
if (e.target.selectedIndex && e.target.selectedIndex < 4 && bench.selectedIndex > e.target.selectedIndex)
bench.selectedIndex = 0;
});
});
// Set 17X
document.addEventListener("DOMContentLoaded", e => {
'use strict';
const combo = document.getElementById("17X-Drawer-Shelf-Combo"),
bench = document.getElementById("17X-Under-Drawer-Bench");
combo.addEventListener("input", e => {
for (let i = 0; i < bench.children.length; i++)
bench.children[i].hidden = e.target.selectedIndex < 4 && i > e.target.selectedIndex;
if (e.target.selectedIndex && e.target.selectedIndex < 4 && bench.selectedIndex > e.target.selectedIndex)
bench.selectedIndex = 0;
});
});
Any advice would be much appreciated!