0

I am trying to print select in multiple drop-down list, but prints only two words(CHENNAI AND IOS-XE , BANGALORE AND IOS-XR) could not print the remaining words Can anyone help me fix this?

Required Output :

if i select CHENNAI+IOS-XE ---> output : CHENNAI AND IOS-XE

else if BANGALORE+IOS-XR ---> output : BANGALORE AND IOS-XR

else if CHENNAI+IOS-XR ---> output : CHENNAI AND IOS-XR

else if BANGALORE+IOS-XE ---> output : BANGALORE AND IOS-XE

<html>
<body>
  <p>SELECT THE REGION </p>
  <select id="choose">
    <option value="a1">CHENNAI</option>
    <option value="b1">BANGALORE</option>
  </select></p>
  
  <p>SELECT THE ROUTER TYPE </p>
  <select id="choose1">
    <option value="a2">IOS-XE</option>
    <option value="b2">IOS-XR</option>
  </select></p>
  
  <button onclick="button()">CLICK</button>

  <script>
    function button() {
      var x = document.getElementById("choose").value;
      var y = document.getElementById("choose1").value;
      if (x == 'a1' || y == 'a2' ) {
        document.write("CHENNAI AND IOS-XE");
      }
      else if (x == 'b1' || y == 'b2'){
        document.write("BANGALORE AND IOS-XR");
      }
      else if (x == 'a1' || y == 'b2'){
        document.write("CHENNAI AND IOS-XR");
      }   
      else if (x == 'b1' || y == 'a2'){
        document.write("BANGALORE AND IOS-XE");
      }       
    }
  </script>
</body>
</html>

2 Answers 2

1

Your else statement cannot contain a condition check.

You need to change the logic to the following:

if (x == 'a1') {
  document.write("A SELECTED");
}
else if (x == 'b1'){
//   ^  added this :)
  document.write("B SELECTED");
}
Sign up to request clarification or add additional context in comments.

Comments

1

else shouldn't have any conditions in it, if you want to use a condition for it you should use else if:

else if ( x == 'b1' ) {
    // Your statement
} 

or you can use a general else for all other cases:

else {
    // Your statement
}

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.