1

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>

4
  • Are there any errors in the console? You don' thave any elements with id="#div", so this line will fail: var mydiv = document.getElementById('#div');. You'll need to use the input's value to determine which div is mydiv and change its display, but you'll probably also want to hide the remaining div. Commented Jun 23, 2022 at 13:40
  • You need to get var mydiv = document.getElementById('#' + this.value) and do the toggle of the rest, as mentioned by @user1599011, by creating a loop. Commented Jun 23, 2022 at 13:51
  • 1
    you are using getElementById why you are putting # inside Commented Jun 23, 2022 at 14:00
  • Arrghh, of course you need to var mydiv = document.getElementById(this.value). Nice catch prince of the forest! Commented Jun 23, 2022 at 14:33

2 Answers 2

1

your script will be like this.

var myinput = document.getElementById('example');
var divs = document.querySelectorAll('.hidden');
myinput.addEventListener('change', function() {
    var mydiv = document.getElementById(this.value);
    divs.forEach(div => {
      div.style.display = div.id === this.value ? 'block' : 'none';
    });
});
.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>

Sign up to request clarification or add additional context in comments.

Comments

1

If you have a selected element, remove .hidden to show it.

Else, search for the div that doesn't have .hidden and add back the class to hide it.

document.getElementById('example').addEventListener("change", () => {
  if(event.target.value){
    document.getElementById(event.target.value).classList.remove("hidden");
  }
  else{
    document.querySelector(".somediv:not(.hidden)").classList.add("hidden");
  }
});
.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="somediv hidden" id="div1">This is div1</div>
<div class="somediv hidden" id="div2">This is div2</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.