One thing that might be nice here would be the in-line if statement:
(boolean)? true: false
Basically you write the query, then a ‘?’ then what you want the value to be if true, then ‘:’ then what you want the value to be if false.
If you haven’t gotten to them yet you may want to look into them. Ended up being one of my favorite things when I got to that part in the class. Then you could write-
You can cache the button element value too, so you dont select it each time you need its value. You could also use switch block, because its more exact when comparing the value of single var multile times.
function showDivs() {
var divOne = document.getElementById("optionOne");
var divTwo = document.getElementById("optionTwo");
var divThree = document.getElementById("optionThree");
const selectValue = document.getElementById('selectDiv').value
switch(selectValue) {
case '1':
divOne.style.display = "block";
divTwo.style.display = "none";
divThree.style.display = "none";
break;
case '2':
divOne.style.display = "none";
divTwo.style.display = "block";
divThree.style.display = "none";
break;
case '3':
divOne.style.display = "none";
divTwo.style.display = "none";
divThree.style.display = "block";
break;
default:
divOne.style.display = "none";
divTwo.style.display = "none";
divThree.style.display = "none";
}
}
It is also recommended to change styles via classes and not modify inline styles via element.style, for example in your code:
case '1':
divOne.classList.remove('hiddenDiv')
divTwo.classList.add('hiddenDiv')
divThree.classList.add('hiddenDiv')
break;
Should produce similar effect(altho it can be refiend even more, as we wont always need to add the class to every other element.
And then, how i would prolly approach this task:
I would use a single element as a color box, which would change its color based on the selected option. I would use classes to handle that.
HTML: