Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
How to show a div when a select option is selected and hide when unselected?

I have two select menus. One menu is hidden. If a specific option is selected from the Menu 1, then Menu 2 will display. If a selection not part of the condition is selected, then Menu 2 should go away.

If I select Defendant, Minor, or Witness, then Menu 2 stays hidden (as intended).

If I select Social Services, then Menu 2 will display. If I select Prosecutor's Office, then Menu 2 doesn't display.

If I just select Prosecutor's Office, then nothing displays. It is intended to display Menu 2.

I'm not sure what I'm doing wrong.

Am I not allowed to use || in a ternary statement?

document.getElementById("form1").addEventListener("change", function () {
  var style = this.value == (4 || 5) ? "block" : "none";
  document.getElementById("form2").style.display = style;
});
<select id="form1" name="form1">
  <option value="1">Defendant</option>
  <option value="2">Minor</option>
  <option value="3">Witness</option>
  <option value="4">Social Services</option>
  <option value="5">Prosecution</option>
</select>
<select id="form2" name="form2" style="display: none;">
  <option value="1">Legal Services</option>
  <option value="2">Social Services</option>
  <option value="3">Prosecutor's Office</option>
</select>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

(4 || 5) will always evaluate to true and this.value == true isn't what you want.

Check the conditions separately (this.value == 4 || this.value == 5)

document.getElementById("form1").addEventListener("change", function () {
  var style = (this.value == 4 || this.value == 5) ? "block" : "none";
  document.getElementById("form2").style.display = style;
});
<select id="form1" name="form1">
  <option value="1">Defendant</option>
  <option value="2">Minor</option>
  <option value="3">Witness</option>
  <option value="4">Social Services</option>
  <option value="5">Prosecution</option>
</select>
<select id="form2" name="form2" style="display: none;">
  <option value="1">Legal Services</option>
  <option value="2">Social Services</option>
  <option value="3">Prosecutor's Office</option>
</select>

about 4 years ago · Juan Pablo Isaza Relatório

0

You have to use the || operator between two comparations (not two values).

document.getElementById("form1").addEventListener("change", function () {
  var style = this.value == 4 || this.value == 5 ? "block" : "none";
  document.getElementById("form2").style.display = style;
});
<select id="form1" name="form1">
  <option value="1">Defendant</option>
  <option value="2">Minor</option>
  <option value="3">Witness</option>
  <option value="4">Social Services</option>
  <option value="5">Prosecution</option>
</select>
<select id="form2" name="form2" style="display: none;">
  <option value="1">Legal Services</option>
  <option value="2">Social Services</option>
  <option value="3">Prosecutor's Office</option>
</select>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda