1

My else if statements are not working, it always shows 2 boxes.

function testFunction() {
  var x = document.getElementById("selectAmount").value;
  if (x = 2) {
    $('#box1').show();
    $('#box2').show();
    $('#box3').hide();
    $('#box4').hide();
  } else if (x = 3) {
    $('#box1').show();
    $('#box2').show();
    $('#box3').show();
    $('#box4').hide();
  } else if (x = 4) {
    $('#box1').show();
    $('#box2').show();
    $('#box3').show();
    $('#box4').show();
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="selectAmount" onchange="testFunction()">
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
 </select>

If anyone would love to explain how else if statements work, would be greatly appreciated.

Younes Regaieg
  • 4,156
  • 2
  • 21
  • 37
LeEclipse
  • 31
  • 4

0 Answers0