This has probably been asked about before, but after several hours of looking I can't seem to find anything.
I have a datalist which is meant to display a div based on your selection of the datalist. Whatever I do, I can't seem to find out how to do it. The best I can get to work is have both selections display one of the divs, but not have spesific divs display based on the selection.
How do I make it so choosing div1 display div1, div2 displays div2 and so on?
var myinput = document.getElementById('example');
myinput.addEventListener("change", function() {
var mydiv = document.getElementById('#div');
mydiv.style.display = (this.value === '' ? 'none' : 'inline');
});
.hidden {
display: none;
}
<fieldset>
<legend>This is a datalist</legend>
<input type="text" id="example" list="brow" />
<datalist id="brow">
<option value="div1" />
<option value="div2" />
</datalist>
</fieldset>
<div class="hidden" id="div1">This is div1></div>
<div class="hidden" id="div2">This is div2></div>
var mydiv = document.getElementById('#div');. You'll need to use the input's value to determine which div ismydivand change its display, but you'll probably also want to hide the remaining div.var mydiv = document.getElementById('#' + this.value)and do the toggle of the rest, as mentioned by @user1599011, by creating a loop.var mydiv = document.getElementById(this.value). Nice catch prince of the forest!