Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!